LeetCode · Trees

Trees Problem Set

Binary trees, BSTs, and tree traversals. Most tree problems are recursive — think "what does each node need from its children?"

Concept pages: Binary Tree · BST
DifficultyPatternProblemKey Insight
EasyrecursionLC 226 · Invert Binary TreeSwap left and right children recursively. O(n).
EasyrecursionLC 104 · Maximum Depth of Binary Tree1 + max(left depth, right depth). O(n).
EasyrecursionLC 101 · Symmetric TreeMirror recursion compares left.left with right.right and left.right with right.left.
EasyrecursionLC 100 · Same TreeCompare structure and values recursively. O(n).
EasyrecursionLC 572 · Subtree of Another TreeCheck isSameTree at every node. O(m·n).
EasytraversalLC 94 · Binary Tree Inorder TraversalLeft → root → right. Iterative with stack or recursive. O(n).
MediumBSTLC 98 · Validate Binary Search TreePass min/max bounds down. O(n).
MediumBFSLC 102 · Binary Tree Level Order TraversalBFS with queue, group by level. O(n).
MediumconstructionLC 105 · Construct Binary Tree from Preorder and InorderPreorder root + inorder split. HashMap for O(1) index lookup. O(n).
MediumLCALC 236 · Lowest Common Ancestor of a Binary TreeIf one target is in each subtree, the current node is the first split point and therefore the LCA.
MediumBSTLC 230 · Kth Smallest Element in a BSTInorder traversal, count to k. O(H + k).
MediumLCALC 235 · Lowest Common Ancestor of a BSTIf both < root go left, both > root go right, else root is LCA. O(H).
HardpathLC 124 · Binary Tree Maximum Path SumAt each node: max single-path vs split-path. Track global max. O(n).

← Back to all LeetCode categories