Skip to main content

Simply Traverse

Height

559. Maximum Depth of N-ary Tree

Easy·

Solutions:
FIG. MAXIMUM DEPTH OF N ARY TREE INTERACTIVE
def maxDepth(self, root: "Node") -> int:
def rec(node):
if not node:
return 0
child_max_depth = 0
for child in node.children:
child_max_depth = max(child_max_depth, rec(child))
return 1 + child_max_depth
 
return rec(root)

Find Root

1506. Find Root of N-Ary Tree

Medium·

Solutions:
FIG. FIND ROOT OF N ARY TREE INTERACTIVE
def findRoot(self, tree: List["Node"]) -> "Node":
seen = set()
for node in tree:
for child in node.children:
seen.add(child.val)
for node in tree:
if node.val not in seen:
return node