dsa3 min read
Sum of Distances in Tree
Compute sum of distances from every node to all others in O(n) using two DFS passes with rerooting technique.
Read →
1575 articles
Compute sum of distances from every node to all others in O(n) using two DFS passes with rerooting technique.
Count the number of structurally unique BSTs with n nodes using Catalan numbers and DP.
Perform level-order traversal of an N-ary tree using BFS, collecting all children at each level.
Remove all subtrees that do not contain a 1 by recursively returning null for subtrees with only zeros.
Find the level with the maximum sum using BFS level-order traversal to compute each level's total.