Tree
LeetCode 701: Insert into a Binary Search Tree
·2 mins
LeetCode
Medium
Tree
Binary-Search-Tree
Binary-Tree
LeetCode 701 note: walk down the BST to an empty spot and attach the new node; an empty tree must return the new node
LeetCode 687: Longest Univalue Path
·2 mins
LeetCode
Medium
Tree
Depth-First-Search
Binary-Tree
Dp-on-Trees
LeetCode 687 note: DFS returns the longest same-value downward chain in edges, and each node updates the answer with left + right
LeetCode 515: Find Largest Value in Each Tree Row
·1 min
LeetCode
Medium
Tree
Depth-First-Search
Breadth-First-Search
Binary-Tree
LeetCode 515 note: BFS level order, taking the maximum of each level; start from -inf
LeetCode 429: N-ary Tree Level Order Traversal
·1 min
LeetCode
Medium
Tree
Breadth-First-Search
LeetCode 429 note: BFS level order with the children list in place of left and right
LeetCode 173: Binary Search Tree Iterator
·2 mins
LeetCode
Medium
Stack
Tree
Design
Binary-Search-Tree
Binary-Tree
Iterator
LeetCode 173 note: flattening to an array is simple but uses O(n) space; the follow-up keeps the left spine on a stack for amortized O(1) and O(h) space
LeetCode 1372: Longest ZigZag Path in a Binary Tree
·2 mins
LeetCode
Medium
Dynamic-Programming
Tree
Depth-First-Search
Binary-Tree
Dp-on-Trees
LeetCode 1372 note: each node returns two values, the longest zigzag starting left and starting right; going left extends the left child’s start-right value
LeetCode 124: Binary Tree Maximum Path Sum
·2 mins
LeetCode
Hard
Dynamic-Programming
Tree
Depth-First-Search
Binary-Tree
Dp-on-Trees
LeetCode 124 note: DFS returns the best downward chain and updates the answer with left chain + node + right chain at every node; drop negative chains
LeetCode 3787: Find Diameter Endpoints of a Tree
·2 mins
LeetCode
Medium
Tree
Breadth-First-Search
Graph
LeetCode 3787 note: two BFS passes find one diameter’s ends A and B; every node’s farthest node is A or B, so a node is a diameter endpoint exactly when max(dist(A,i), dist(B,i)) equals the diameter
LeetCode 314: Binary Tree Vertical Order Traversal
·2 mins
LeetCode
Medium
Hash-Table
Tree
Depth-First-Search
Breadth-First-Search
Sorting
Binary-Tree
LeetCode 314 note: BFS with a column index; BFS is what guarantees top-to-bottom within a column and left-to-right within a row
Tree Traversal and Common Techniques
·11 mins
Algorithm
Tree
Binary-Tree
Dfs
Bfs
Bst
DFS return values, BFS level order, BST bounds, climbing to the LCA, tree diameter, and leaf trimming
LeetCode 3203: Find Minimum Diameter After Merging Two Trees
·3 mins
LeetCode
Hard
Tree
Bfs
Dfs
Graph
Tree-Diameter
LeetCode 3203 note: connect the two centers; the answer is max(d1, d2, ceil(d1/2) + ceil(d2/2) + 1)
LeetCode 3313: Find the Last Marked Nodes in Tree
·2 mins
LeetCode
Hard
Tree
Bfs
Graph
Tree-Diameter
LeetCode 3313 note: the last marked node is the farthest one, and a farthest node can always be a diameter endpoint