Tree
LeetCode 637: Average of Levels in Binary Tree
·1 min
LeetCode
Easy
Tree
Depth-First-Search
Breadth-First-Search
Binary-Tree
LeetCode 637 note: level-order traversal, dividing each level’s sum by its count
LeetCode 606: Construct String from Binary Tree
·1 min
LeetCode
Medium
String
Tree
Depth-First-Search
Binary-Tree
LeetCode 606 note: output in pre-order, keeping an empty () when only the left subtree is missing, and omitting the right parentheses when it is empty
LeetCode 590: N-ary Tree Postorder Traversal
·1 min
LeetCode
Easy
Stack
Tree
Depth-First-Search
LeetCode 590 note: post-order is each child then the node; iteratively, run a root-then-children-right-to-left pre-order and reverse the whole result
LeetCode 589: N-ary Tree Preorder Traversal
·2 mins
LeetCode
Easy
Stack
Tree
Depth-First-Search
LeetCode 589 note: n-ary pre-order records the node then each child in order; flatten recursive results with extend, not by appending a list
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 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