mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Update trie_find_height.py
This commit is contained in:
parent
761e86705f
commit
a97c06f579
@ -15,25 +15,27 @@ def max_depth_recursive(root):
|
|||||||
if root is None:
|
if root is None:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
if root.children == []:
|
if root.children: is None:
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
height = [max_depth_recursive(c) for c in root.children]
|
height = [max_depth_recursive(children) for children in root.children]
|
||||||
|
|
||||||
return max(height) + 1
|
return max(height) + 1
|
||||||
|
|
||||||
|
|
||||||
def max_depth_iterative(root):
|
def max_depth_iterative(root):
|
||||||
|
|
||||||
stack = []
|
stack, depth = [], 0
|
||||||
|
|
||||||
if root is not None:
|
if root is not None:
|
||||||
stack.append((1, root))
|
stack.append((1, root))
|
||||||
|
|
||||||
depth = 0
|
while stack:
|
||||||
|
|
||||||
while stack != []:
|
|
||||||
|
|
||||||
this_depth, node = stack.pop()
|
this_depth, node = stack.pop()
|
||||||
|
|
||||||
if node is not None:
|
if node is not None:
|
||||||
|
|
||||||
depth = max(depth, this_depth)
|
depth = max(depth, this_depth)
|
||||||
for c in node.children:
|
for c in node.children:
|
||||||
stack.append((this_depth + 1, c))
|
stack.append((this_depth + 1, c))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user