Tree Modification
DFS First
Mirror Tree
Mirror a binary tree by swapping the left and right subtrees of every node. The mirror operation transforms the tree so that the left child becomes the right child and vice versa.
- Time
- O(n)
dfsvisits each node once to swap its children.- Space
- O(h)
- The recursion stack depth equals tree height
h.
Exchange the Leaf Nodes
Pairwise swap all leaf nodes of a binary tree. If there are odd number of leaf nodes, the last leaf node remains unchanged.
- 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.
617. Merge Two Binary Trees
Overlay the two trees: where both have a node, the merged node holds the sum of their values; where only one has a node, that subtree is carried over unchanged. A parallel DFS walks both trees in lockstep, building the merged tree node by node.
- Time
- O(m + n)
recursiononly stops (return None) once bothaandbareNone; whenever either tree still has a node, it keeps descending and copying that side over. So every node of bothroot1(sizem) androot2(sizen) is visited exactly once -m + n.- Space
- O(h)
- The recursion keeps descending into whichever tree is deeper even after the other side runs out, so the call stack grows to the height
hof the taller of the two trees.
Create a New Tree
654. Maximum Binary Tree
Build the tree recursively: the largest value in the current range becomes the root, the elements to its left form the left subtree, and the elements to its right form the right subtree. Repeating this on each half constructs the maximum binary tree.
- Time
- O(n^2)
find_maxscans its[lo, hi)range, andrecursioncalls it once per node - in the worst case (sorted input) each call only shrinks the range by one, givingn + (n - 1) + ... + 1,O(n^2).- Space
- O(n)
- The recursion stack reaches depth
nin the worst case, when the input is already sorted and the tree is fully skewed.
BFS First
116. Populating Next Right Pointers in Each Node
Given a perfect binary tree, populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
FOLLOW UP: You may only use constant extra space.

- Time
- O(n)
- Each of the
nnodes is popped fromqueueand processed exactly once. - Space
- O(n)
queueholds at mostn/2nodes at once - the widest (last) level of a perfect binary tree.
117. Populating Next Right Pointers in Each Node II
TODO: Undertsnad the optimal approach with constact auxillary space
Given a binary tree, populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
FOLLOW UP: You may only use constant extra space.
- Time
- O(n)
nis the number of nodes in the tree.- Each node is popped from
queueand processed exactly once, withO(1)work per node. - Space
- O(n)
queueholds every node of the widest level at once, which can grow up toO(n)nodes in the worst case (e.g. a perfect tree's last level holds close ton/2nodes).