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