Height, Path, Views
Height
104. Maximum Depth of Binary Tree
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. An empty tree has depth 0, a single node has depth 1.
- Time
- O(n)
dfsvisits each of thennodes exactly once.- Space
- O(h)
- The recursion call stack grows one frame per level, so it never exceeds the tree's height
h.
111. Minimum Depth of Binary Tree
Find the minimum depth from the root to any leaf node. The minimum depth is the shortest path from root to leaf.
- Time
- O(n)
dfsvisits each of thennodes exactly once, doing constant work per call.- Space
- O(h)
- The recursion call stack grows one frame per level, up to the tree height
h.
Path
112. Path Sum
112Path Sum
Determine if a binary tree has a root-to-leaf path whose sum equals the given target value. A leaf is a node with no children.
- Time
- O(n)
dfsvisits each of thennodes exactly once.- Space
- O(h)
- The recursion call stack holds at most
hframes, one per level fromrootdown to the current leaf.
113. Path Sum II
113Path Sum II
Find all root-to-leaf paths where the sum equals the target value. Return all valid paths as a list of lists containing node values.
- Time
- O(n * h)
dfsvisits each of thennodes once, but every leaf match callsdeepcopy(path), which costsO(h)sincepathholds one value per level of the current root-to-leaf chain.- Space
- O(n * h)
- The recursion stack is
O(h)deep, butresultcan hold up toncopied paths, each up to lengthh, in the worst case.
257. Binary Tree Paths
Return all root-to-leaf paths in a binary tree as strings, where each path shows the values connected by "->".
- Time
- O(n * h)
dfsvisits each of thennodes once, but at every leafdeepcopy(path)copies the current path, whose length is bounded by the tree heighth, givingO(n * h)overall.- Space
- O(n * h)
- The recursion stack and
pathare bounded byh, andresultstores a deep copy of a path (length up toh) for each of up tonleaves.
129. Sum Root to Leaf Numbers
Each root-to-leaf path spells a decimal number (most significant digit at the root). Carry the running value down with cur_decimal = 10 * cur_decimal + node.val, and add it to the total whenever a leaf is reached. This mirrors the binary version (1022), swapping base 2 for base 10.
- Time
- O(n)
recursionvisits each node exactly once viarecursion(node.left)andrecursion(node.right), doingO(1)work per node, so total isO(n), wherenis the number of nodes.- Space
- O(h)
- The recursion stack (plus the shared
nonlocal cur_decimal) goes as deep as the tree, so it holds at mosthframes, wherehis the tree height.
1022. Sum of Root To Leaf Binary Numbers
Each root-to-leaf path spells a binary number (most significant bit at the root). Carry the running value down the path with cur_binary = 2 * cur_binary + node.val, and add it to the total whenever a leaf is reached.
- Time
- O(n)
- Each node is visited once.
- Space
- O(h)
- Recursion stack depth equals tree height
h.
1457. Pseudo-Palindromic Paths in a Binary Tree
A root-to-leaf path is pseudo-palindromic if its node values can be rearranged into a palindrome - which happens exactly when at most one digit value appears an odd number of times. Track parity with a bitmask: toggle bit node.val on each step (path ^ (1 << node.val)). At a leaf the path is valid when the mask has at most one bit set, i.e. path & (path - 1) == 0.
- Time
- O(n)
recursionvisits each of thennodes exactly once.- Space
- O(h)
- The recursion call stack depth equals tree height
h.
1026. Maximum Difference Between Node and Ancestor
The largest |ancestor - descendant| along any root-to-node path equals max - min of the values on that path. Carry the running max_node and min_node down each path; the answer at any point is abs(max_node - min_node), and the overall answer is the largest such value across all paths.
- Time
- O(n)
recursionvisits each of the tree'snnodes exactly once.- Space
- O(h)
- The recursion call stack grows with the tree's height
h.
Views
Left View of Binary Tree
The left view of a binary tree is the set of nodes visible when the tree is viewed from the left side. In other words, it's the first node at each level when traversing from left to right.
- Time
- O(n)
dfsvisits each of thennodes exactly once.- Space
- O(h)
- The recursion stack holds one frame per level on the current path, where
his the tree height.left_viewadds one entry per level, at mosth, dominated by the same term.
Right View of Binary Tree
The right view of a binary tree is the set of nodes visible when the tree is viewed from the right side. In other words, it's the last node at each level when traversing from left to right.
- Time
- O(n)
nis the number of nodes -dfsvisits every node exactly once, recording the last-seen value at each level.- Space
- O(h)
his the tree height - the recursion stack indfsgrows one frame per level, plusright_viewholds one entry per level (O(h)), which does not exceed the stack depth.
Top View of Binary Tree
The top view of a binary tree is the set of nodes visible when the tree is viewed from the top. Each column position should show only the topmost node (closest to root level).
- Time
- O(n)
dfsvisits each of thennodes exactly once; the final list comprehension overrange(min_col, max_col + 1)is bounded by the tree's column width, which is at mostn.- Space
- O(n)
- The recursion stack goes as deep as the tree (up to
nfor a skewed tree), andtop_viewholds at most one entry per column, up tonentries.
Bottom View of Binary Tree
The bottom view of a binary tree is the set of nodes visible when the tree is viewed from the bottom. Each column position should show only the bottommost node (farthest from root level).
- Time
- O(n)
dfsvisits each of thennodes exactly once.- Space
- O(n)
- The recursion stack reaches depth up to the tree's height (bounded by
n), andbottom_viewholds one entry per distinct column, at mostnentries.
545. Boundary of Binary Tree
The boundary of a binary tree is the concatenation of root, left boundary, leaves, and right boundary in counter-clockwise direction.
- Left boundary: path from root to the left-most node
- Right boundary: path from root to the right-most node
- Leaves: all leaf nodes in left-to-right order
- Time
- O(n)
setLeavesvisits every node in the tree once;setLeftBoundaryandsetRightBoundaryeach only walk a boundary spine (O(h)), which is dominated by theO(n)leaves pass.- Space
- O(h)
- The deepest recursion is
setLeaves, whose call stack grows to the tree's heighth.
