Simply Traverse
Height
559. Maximum Depth of N-ary Tree
Easy·
3 Approaches
DFS (Recursive)
O(n)O(h)
DFS (Iterative)
O(2n)O(n)
BFS (Iterative)
O(n)O(w)
FIG. MAXIMUM DEPTH OF N ARY TREE● INTERACTIVE
visualization loads as you reach it
Find Root
1506. Find Root of N-Ary Tree
Medium·
3 Approaches · 2 patterns
Seen Set
O(2n)O(n)
Counter
O(n)O(n)
XOR↗ XOR
O(2n)O(1)
FIG. FIND ROOT OF N ARY TREE● INTERACTIVE
visualization loads as you reach it
Weighted Sum
4015. Weighted Sum of a Tree
Medium·
The tree arrives as a parent array (parent[i] = -1 marks the root). Each node's weight is tree_height - depth + 1, so the shallowest nodes count for the most. Return the sum of nums[i] times its weight.
2 Approaches
BFS (Iterative)
O(4n)O(3n + w)
Memoized Parent Walk (Recursive)
O(4n)O(n + h)
FIG. WEIGHTED SUM OF A TREE● INTERACTIVE
visualization loads as you reach it