The Populating Next Right Pointers in Each Node II extends the first problem to any binary tree (not just perfect). Each node may have 0, 1, or 2 children. The O(1)-space solution is more complex since you can't assume all nodes at a level have children. The linked list, BFS, DFS, binary tree, and tree interview pattern is demonstrated at an advanced level.
Citadel, Microsoft, Meta, Amazon, Google, and Bloomberg ask this as the harder variant. It tests whether candidates can extend the perfect-tree O(1) solution to handle missing children — requiring a "dummy head" trick for each level.
Dummy head for each level + O(1) space. For each level: use a dummy node levelHead to build the next level's linked list as you traverse the current level. For each node traversed: if it has a left child, add to levelHead chain. If it has a right child, add to chain. After processing the current level, move to the next level's first real node (levelHead.next). Repeat.
Tree: root=1 (children: 2,3). Node 2 (children: 4,5). Node 3 (children: none,7). Level 1 processed: 1.next=null. Next level dummy → chain: 2→3. Level 2 processed (using next pointers 2→3): node 2's children: dummy→4→5. node 3's children: dummy→...→7. Chain: 4→5→7. Level 3 processed: 4.next=5, 5.next=7, 7.next=null.
The dummy head trick for linked list level building is elegant: dummy = ListNode(0); tail = dummy. As you add children to the next level, extend the tail. After processing a level, next_level_head = dummy.next. This pattern generalizes to any "build next level as linked list" tree problem. Practice both variants I and II until the O(1)-space approach feels natural.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Populating Next Right Pointers in Each Node | Medium | Solve | |
| Find Largest Value in Each Tree Row | Medium | Solve | |
| Add One Row to Tree | Medium | Solve | |
| Linked List in Binary Tree | Medium | Solve | |
| Find Bottom Left Tree Value | Medium | Solve |