↓Skip to main content

Binary-Tree

LeetCode 2385: Amount of Time for Binary Tree to Be Infected
·2 mins
LeetCode Medium Hash-Table Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 2385 note: turn the tree into an undirected graph and BFS from start; the number of levels minus one is the answer
LeetCode 968: Binary Tree Cameras
·3 mins
LeetCode Hard Dynamic-Programming Tree Depth-First-Search Binary-Tree Dp-on-Trees
LeetCode 968 note: post-order greedy with three states per node: has a camera, covered, not covered; place a camera only when a child is uncovered, and add one if the root is left uncovered
LeetCode 889: Construct Binary Tree from Preorder and Postorder Traversal
·2 mins
LeetCode Medium Array Hash-Table Divide-and-Conquer Tree Binary-Tree
LeetCode 889 note: keep the current path on a stack; when its top equals the next postorder value that subtree is complete and is popped; new nodes go left first
LeetCode 863: All Nodes Distance K in Binary Tree
·2 mins
LeetCode Medium Hash-Table Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 863 note: turn the tree into an undirected graph (adding edges to parents), then BFS k steps from target
LeetCode 437: Path Sum III
·2 mins
LeetCode Medium Tree Depth-First-Search Binary-Tree
LeetCode 437 note: prefix sums on a tree with a hash map: look up prefix - target before adding the current prefix, and undo it when leaving the node
LeetCode 652: Find Duplicate Subtrees
·2 mins
LeetCode Medium Hash-Table Tree Depth-First-Search Binary-Tree
LeetCode 652 note: encode every subtree in post-order and report it on its second occurrence; string keys are O(n^2) worst case, (val, left id, right id) ids make it O(n)
LeetCode 99: Recover Binary Search Tree
·2 mins
LeetCode Medium Tree Depth-First-Search Binary-Search-Tree Binary-Tree
LeetCode 99 note: find the inversions in the in-order sequence: the earlier node of the first inversion and the later node of the last one were swapped
LeetCode 450: Delete Node in a BST
·2 mins
LeetCode Medium Tree Binary-Search-Tree Binary-Tree
LeetCode 450 note: once the node is found: with zero or one child, splice the child in; with two, copy the right subtree’s minimum and delete that node
LeetCode 449: Serialize and Deserialize BST
·2 mins
LeetCode Medium String Tree Depth-First-Search Breadth-First-Search Design Binary-Search-Tree Binary-Tree
LeetCode 449 note: for a BST, pre-order without null markers is enough; deserialize recursively with bounds in O(n)
LeetCode 331: Verify Preorder Serialization of a Binary Tree
·2 mins
LeetCode Medium String Stack Tree Binary-Tree
LeetCode 331 note: count open slots: start with 1, every node fills one, a non-null node opens two more; never go negative and end at exactly 0
LeetCode 297: Serialize and Deserialize Binary Tree
·2 mins
LeetCode Hard String Tree Depth-First-Search Breadth-First-Search Design Binary-Tree
LeetCode 297 note: pre-order with null markers identifies the tree uniquely; deserialize by consuming tokens from an iterator
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