Linked List Meets Tree
A binary tree node holds two pointers and a linked list node holds one, so a
tree already has the wiring a list needs. Every problem on this page leaves the
values alone and rewires pointers in place. What changes is which pointer
becomes the list's next, and how many lists you end up with.
114. Flatten Binary Tree to Linked List
is the hub: one list, preorder, reusing right. The other three change one
thing each. The opposite direction, a list hiding inside a tree's .parent
pointers, lives on Intersection of Two Chains.
One list, from the tree's own pointers
right, left clearedThe tree's right pointer becomes the list's next and left is set to
None. The order is preorder: node, then its left subtree, then its right.
The first two solutions rebuild the chain as the recursion returns, one by
walking with a sentinel and one by returning each subtree's tail. The iterative
stack is the same walk with the recursion made explicit. The Morris-like
solution is the one to study: for each node with a left child, hang its right
subtree on the rightmost node of the left subtree, then move the whole left
subtree over to the right. No stack, no recursion, O(1) space.
114. Flatten Binary Tree to Linked List
114. Flatten Binary Tree to Linked List
Sentinel + Preorder
right, left cleared897. Increasing Order Search Tree
is 114 with the visit order swapped. Same right as next, same left
cleared, same single list. Only the traversal changes: inorder instead of
preorder.
On a BST, inorder visits values smallest to largest, so the list comes out sorted without any sorting step. The first solution allocates a fresh node per value; the second relinks the existing nodes, which is a sentinel walk like 114's with the work done between the two recursive calls. Order is the knob: preorder gives a list in the tree's own shape, inorder gives a sorted one.
897. Increasing Order Search Tree
897. Increasing Order Search Tree
Recursive (Create new tree)
One list per level
next field116. Populating Next Right Pointers in Each Node
is 114 minus the single list. The tree edges stay, and a new next field links
each level into its own list, left to right.
Four solutions, and the last is the one that earns the page its place. The
level above is already a linked list, so use it as the walker: with head
moving along level d through next, set head.left.next = head.right and
head.right.next = head.next.left for level d + 1. No queue, O(1) space,
and the linked list is both the thing being built and the tool building it.
116. Populating Next Right Pointers in Each Node
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.

BFS
next field117. Populating Next Right Pointers in Each Node II
is 116 without the guarantee that every node has two children. Once children
can be missing, head.right.next = head.next.left can point at nothing, so the
shortcut from 116 no longer works as written.
The solution on this page is the BFS, which does not care about shape: each level is read off the queue and linked left to right. The constant-space version walks the level above the same way as in 116, but has to skip over gaps to find the next real child. It is not written up here yet.
117. Populating Next Right Pointers in Each Node II
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.
BFS
The four problems side by side:
| Problem | Becomes `next` | Visit order | Lists built | Best space |
|---|---|---|---|---|
| 114 Flatten Binary Tree to Linked Listthe hub | right, with left set to None | Preorder | One list | O(1)thread the left subtree in, no stack |
| 897 Increasing Order Search Treefrom 114 | right, with left set to None | Inorder, so the list comes out sorted | One list | O(h)the recursion stack |
| 116 Populating Next Right Pointers in Each Nodefrom 114 | The next field, tree edges untouched | Level by level | One list per level | O(1)walk the level above through its own next |
| 117 Populating Next Right Pointers in Each Node IIfrom 116 | The next field, tree edges untouched | Level by level | One list per level | O(w)the page's BFS; children can be missing |