Simply Traverse
Simply DFS
Sum of Binary Tree
Calculate the sum of all node values in a binary tree by traversing each node and adding its value to the total sum.
- Time
- O(n)
dfsvisits each of thennodes exactly once.- Space
- O(h)
- The recursion call stack grows with the tree's height
h.
Size of Binary Tree
Count the total number of nodes in a binary tree. Each node contributes 1 to the count, regardless of its value.
- Time
- O(n)
dfsis called once per node,nbeing the total node count.- Space
- O(h)
- The recursion stack depth equals the tree height
h.
Count Leaves in Binary Tree
Count leaf nodes (nodes with no children) in a binary tree. A leaf node has both left and right children as null.
- Time
- O(n)
nis the number of nodes -dfsvisits every node exactly once.- Space
- O(h)
his the tree height - the recursion stack indfsgrows one frame per level.
Count Non-Leaf Nodes in Tree
Count internal nodes (non-leaf nodes) in a binary tree. A non-leaf node has at least one child (left or right).
- Time
- O(n)
dfsvisits each of thennodes exactly once, summing1for every node with at least one child.- Space
- O(h)
- The recursion stack goes one frame deep per level, up to the tree height
h.
Sum of Leaf Nodes
Calculate the sum of all leaf node values in a binary tree. A leaf node has no children (both left and right are null).
- Time
- O(n)
nis the number of nodes -dfsvisits every node exactly once.- Space
- O(h)
his the tree height - the recursion stack indfsgrows one frame per level.
Max and min element in Binary Tree
Find the maximum and minimum values among all nodes in a binary tree by comparing each node's value during traversal.
- Time
- O(n)
dfsvisits every node exactly once, called twice (once forfindMax, once forfindMin), so it's2ncollapsed toO(n).- Space
- O(h)
- The recursion call stack grows to the tree's height
h.
Vertical Width of a Binary Tree
Find the vertical width of a binary tree. The vertical width is the number of vertical columns needed to display the tree, where each node is assigned a column based on its horizontal distance from the root.
- Time
- O(n)
dfsvisits each of thennodes exactly once, updatingmini/maxiat each.- Space
- O(h)
- The recursion stack goes one frame deep per level, up to the tree height
h.
1469. Find All The Lonely Nodes
A lonely node is a node that is the only child of its parent node. Find all lonely nodes in a binary tree. The root node is never lonely as it has no parent.
- 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.
965. Univalued Binary Tree
A binary tree is univalued if every node in the tree has the same value. Check if a given binary tree is univalued.
- 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.
404. Sum of Left Leaves
Given the root of a binary tree, return the sum of all left leaves. A left leaf is a leaf which is the left child of another node.
- Time
- O(n)
dfsvisits each node exactly once.- Space
- O(h)
- The recursion call stack grows with tree height
h.
1315. Sum of Nodes with Even-Valued Grandparent
Given the root of a binary tree, return the sum of values of nodes with even-valued grandparent. A grandparent is the parent of a parent of a node.
- 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, so it never exceeds the tree's height
h.
671. Second Minimum Node In a Binary Tree
- Time
- O(n)
recvisits each of thennodes exactly once.- Space
- O(h)
- The recursion call stack grows with the tree's height
h.
Simply BFS
637. Average of Levels in Binary Tree
Given the root of a binary tree, return the average value of the nodes on each level in the form of an array.
- Time
- O(n)
- Each of the
nnodes is popped fromqueueand processed exactly once. - Space
- O(w)
queueholds at most one full level, bounded by the tree's max widthw.
1302. Deepest Leaves Sum
Given the root of a binary tree, return the sum of values of its deepest leaves.
- Time
- O(n)
- Each node is popped from
queueand processed exactly once. - Space
- O(w)
queueholds one complete level at a time, so it grows tow, the tree's maximum width.
1161. Maximum Level Sum of a Binary Tree
Given the root of a binary tree, return the number of the level that has the maximum sum (1-indexed).
- Time
- O(n)
- Each of the
nnodes is popped fromqueueand its value added intototalexactly once. - Space
- O(w)
queueholds one complete level at a time, bounded by the tree's maximum widthw.
Max Level Sum in Binary Tree
Find the level in a binary tree that has the maximum sum of node values. Return the maximum sum found across all levels.
- Time
- O(n)
- Every node is enqueued and dequeued exactly once while accumulating each level's
total. - Space
- O(w)
queueholds all nodes of one level at a time, so it never exceeds the tree's maximum widthw.
Largest value in each level
Find the largest value in each level of a binary tree and return them as a list.
- Time
- O(n)
- Each node is popped off
queueand visited exactly once. - Space
- O(w)
queueholds one complete level at a time, bounded by the tree's max widthw.
Sum of Leaf Nodes at Min Level
Find the minimum level where leaf nodes exist, then sum all leaf node values at that specific level. A leaf node has no left or right children.
- Time
- O(n)
- Each of the
nnodes is popped fromqueueand processed exactly once, level by level. - Space
- O(w)
queueholds one full level at a time, wherewis the tree's maximum width.
Odd even level difference
Calculate the sum of all nodes at odd-numbered levels (1, 3, 5, ...) and subtract the sum of all nodes at even-numbered levels (2, 4, 6, ...). Root is at level 1 (odd level).
- Time
- O(n)
- The
while queueloop dequeues every node exactly once viaqueue.popleft(), so total work across all levels isO(n), wherenis the number of nodes. - Space
- O(w)
queueholds at most one full level's worth of nodes - up townodes, wherewis the tree's max width.
Maximum Width of Tree
Find the maximum number of nodes present at any level in a binary tree. The width of a level is the total number of nodes (including null nodes) at that level.
- Time
- O(n)
- Every node is popped from
queueand counted exactly once across allnnodes. - Space
- O(w)
queueholds one full level at a time, so it grows tow, the tree's maximum width.
Maximum Node Level
Find the level in a binary tree that has the maximum number of nodes. Return the level number (0-indexed from root).
- Time
- O(n)
- Each of the
nnodes is popped fromqueueexactly once while summinglengthper level. - Space
- O(w)
queueholds one complete level at a time, up to the tree's maximum widthw.
Level of a Node in Binary Tree
Find the level of a given node in a binary tree. Return the level (1-indexed from root) if the node exists, otherwise return 0.
- Time
- O(n)
queuevisits each of thennodes once, level by level, untiltargetis found (worst case alln).- Space
- O(w)
queueholds one complete level at a time, bounded by the tree's maximum widthw.
Leaves at Same Level or Not
Check if all leaf nodes in a binary tree are at the same level. Return True if all leaves are at the same level, False otherwise.
- Time
- O(n)
queuevisits each of the tree'snnodes exactly once, level by level.- Space
- O(w)
queueholds one complete level at a time, bounded by the tree's maximum widthw.
Nodes at Odd Levels
- Time
- O(n + n log n)
- The BFS visits every node once,
O(n), collecting up tokodd-level values intoresults(worst casek = n). sorted(results)then costsO(k log k), worst caseO(n log n).- Space
- O(sort + n)
queueholds one level at a time, bounded by the tree's widthw <= n;resultsholds up tok <= nvalues.- Sorting algorithms are typically
O(log n)space (in-place, recursion stack only), but Python'ssorted()is Timsort, which allocates up toO(n)auxiliary space in the worst case - that's whatsortstands for here.
Next Right Node
Find the next right node of a given key in a binary tree. The next right node is the node that appears immediately to the right of the given key at the same level. If there's no such node, return a node with value -1.
- Time
- O(n)
- Each node is enqueued and dequeued exactly once level by level.
- Space
- O(w)
queueholds one complete level at a time, so it grows with the tree's max widthw.
1609. Even Odd Tree
1609Even Odd Tree
A binary tree is named Even-Odd if it meets the following conditions:
- The root of the binary tree is at level index 0, its children are at level index 1, their children are at level index 2, etc.
- For every even-indexed level, all nodes at the level have odd integer values in strictly increasing order (from left to right).
- For every odd-indexed level, all nodes at the level have even integer values in strictly decreasing order (from left to right).
Given the root of a binary tree, return true if the binary tree is Even-Odd, otherwise return false.
- Time
- O(n)
- Every node is popped from
queueand checked exactly once across allnnodes. - Space
- O(w)
queueholds one full level at a time, so it grows tow, the tree's maximum width.
513. Find Bottom Left Tree Value
Given the root of a binary tree, return the leftmost value in the last row of the tree.
- Time
- O(n)
- The
while queueloop dequeues every node exactly once viaqueue.popleft(), so total work across all levels isO(n), wherenis the number of nodes. - Space
- O(w)
queueholds at most one full level's worth of nodes - up townodes, wherewis the tree's max width.