↓Skip to main content

Binary-Tree

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 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 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 987: Vertical Order Traversal of a Binary Tree
·2 mins
LeetCode Hard Tree Binary-Tree Bfs Sorting
LeetCode 987 note: collect (col, row, val) triples, sort them, and group by column
LeetCode 1028: Recover a Tree From Preorder Traversal
·3 mins
LeetCode Hard Tree Binary-Tree Dfs Stack String
LeetCode 1028 note: a stack holds the path from the root to the current node, so its length is the depth
LeetCode 2509: Cycle Length Queries in a Tree
·2 mins
LeetCode Hard Tree Binary-Tree Lca
LeetCode 2509 note: in heap numbering the parent is v // 2, so climb the larger node until both meet at the LCA
LeetCode 2196: Create Binary Tree From Descriptions
·2 mins
LeetCode Medium Tree Binary-Tree Hash-Map
LeetCode 2196 note: keep nodes in a dict and link them in one pass; the root is the only value that is nobody’s child
LeetCode 865: Smallest Subtree with all the Deepest Nodes
·2 mins
LeetCode Medium Tree Binary-Tree Bfs Dfs Lca
LeetCode 865 note: BFS to the last level and climb together, or one DFS returning (depth, node)