Depth-First-Search
LeetCode 572: Subtree of Another Tree
·2 mins
LeetCode
Easy
Tree
Depth-First-Search
String-Matching
Binary-Tree
LeetCode 572 note: for every node of root, check whether its subtree equals subRoot; recurse with isSubtree, not by comparing only the children
LeetCode 563: Binary Tree Tilt
·1 min
LeetCode
Easy
Tree
Depth-First-Search
Binary-Tree
Dp-on-Trees
LeetCode 563 note: return subtree sums in post-order; each node’s tilt is the difference of its subtree sums, added to the answer
LeetCode 559: Maximum Depth of N-ary Tree
·1 min
LeetCode
Easy
Tree
Depth-First-Search
Breadth-First-Search
LeetCode 559 note: depth is 1 plus the deepest child, or 1 with no children
LeetCode 543: Diameter of Binary Tree
·2 mins
LeetCode
Easy
Tree
Depth-First-Search
Binary-Tree
Dp-on-Trees
LeetCode 543 note: the diameter may skip the root; return heights in post-order while updating the answer with left + right height at every node, counting edges
LeetCode 530: Minimum Absolute Difference in BST
·1 min
LeetCode
Easy
Tree
Depth-First-Search
Breadth-First-Search
Binary-Search-Tree
LeetCode 530 note: a BST’s in-order is ascending, so the minimum difference is between in-order neighbors; remember the previous value and compare
LeetCode 501: Find Mode in Binary Search Tree
·2 mins
LeetCode
Easy
Tree
Depth-First-Search
Binary-Search-Tree
Binary-Tree
LeetCode 501 note: a BST’s in-order is sorted, so equal values are contiguous; count the current run during in-order, resetting the answer on a new maximum and appending on a tie
LeetCode 463: Island Perimeter
·2 mins
LeetCode
Easy
Array
Depth-First-Search
Breadth-First-Search
Matrix
LeetCode 463 note: each land cell contributes 4 edges, minus 2 for each land neighbor above or to the left
LeetCode 404: Sum of Left Leaves
·1 min
LeetCode
Easy
Tree
Depth-First-Search
Breadth-First-Search
Binary-Tree
LeetCode 404 note: tell each child during DFS whether it is a left child, adding only left children that are leaves
LeetCode 257: Binary Tree Paths
·1 min
LeetCode
Easy
String
Backtracking
Tree
Depth-First-Search
LeetCode 257 note: DFS carrying the path string down, adding the whole path at each leaf
LeetCode 235: Lowest Common Ancestor of a Binary Search Tree
·2 mins
LeetCode
Medium
Tree
Depth-First-Search
Binary-Search-Tree
Binary-Tree
LeetCode 235 note: go left when both p and q are smaller, right when both are larger; otherwise (split, or one is the node itself) this node is the LCA
LeetCode 226: Invert Binary Tree
·1 min
LeetCode
Easy
Tree
Depth-First-Search
Breadth-First-Search
Binary-Tree
LeetCode 226 note: swap every node’s children, using a simultaneous assignment so the already-changed side is not reused
LeetCode 145: Binary Tree Postorder Traversal
·2 mins
LeetCode
Easy
Stack
Tree
Depth-First-Search
Binary-Tree
LeetCode 145 note: iterative post-order pushes a node back with a visited flag on its first pop, then the right and left children; record it on the second pop