Skip to main content

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.

Problem map

One list, from the tree's own pointers​

Otto, your interviewer

114. Flatten Binary Tree to Linked List​

114. Flatten Binary Tree to Linked List

LeetCode
Medium

Sentinel + Preorder

FIG. FLATTEN BINARY TREE TO LINKED LIST● INTERACTIVE
visualization loads as you reach it
class Solution:
def flatten(self, root: Optional[TreeNode]) -> None:
def rec(node):
nonlocal curr
if node:
curr.right = node
curr = curr.right
left, right = curr.left, curr.right
curr.left = curr.right = None
rec(left)
rec(right)
 
sentinel = curr = TreeNode(None)
rec(root)
return sentinel.right
top ↑
Otto, your interviewer

897. Increasing Order Search Tree​

897. Increasing Order Search Tree

LeetCode
Easy

Recursive (Create new tree)

FIG. INCREASING ORDER SEARCH TREE● INTERACTIVE
visualization loads as you reach it
def increasingBST(self, root: TreeNode) -> TreeNode:
def recursion(node):
nonlocal pointer
if node:
recursion(node.left) if node.left else None
pointer.right = TreeNode(node.val)
pointer = pointer.right
recursion(node.right) if node.right else None
 
new_root = pointer = TreeNode(None)
recursion(root)
return new_root.right
top ↑

One list per level​

Otto, your interviewer

116. Populating Next Right Pointers in Each Node​

116. Populating Next Right Pointers in Each Node

LeetCode
Medium

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.

Binary tree with next pointers illustration
Source: Leetcode

BFS

FIG. POPULATING NEXT RIGHT POINTERS BFS● INTERACTIVE
visualization loads as you reach it
def connect(self, root: "Optional[Node]") -> "Optional[Node]":
queue = collections.deque([root])
while queue:
length = len(queue)
for i in range(length):
node = queue.popleft()
if node:
node.next = queue[0] if queue and (i != length - 1) else None
queue.append(node.left) if node.left else None
queue.append(node.right) if node.right else None
return root
top ↑
Otto, your interviewer

117. Populating Next Right Pointers in Each Node II​

117. Populating Next Right Pointers in Each Node II

LeetCode
Medium

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

FIG. POPULATING NEXT RIGHT POINTERS II● INTERACTIVE
visualization loads as you reach it
def connect(self, root: "Node") -> "Node":
queue = collections.deque([root])
while queue:
length = len(queue)
for i in range(length):
node = queue.popleft()
if node:
node.next = queue[0] if queue and (i != length - 1) else None
queue.append(node.left) if node.left else None
queue.append(node.right) if node.right else None
return root
top ↑

The four problems side by side:

ProblemBecomes `next`Visit orderLists builtBest space
114 Flatten Binary Tree to Linked Listthe hubright, with left set to NonePreorderOne listO(1)thread the left subtree in, no stack
897 Increasing Order Search Treefrom 114right, with left set to NoneInorder, so the list comes out sortedOne listO(h)the recursion stack
116 Populating Next Right Pointers in Each Nodefrom 114The next field, tree edges untouchedLevel by levelOne list per levelO(1)walk the level above through its own next
117 Populating Next Right Pointers in Each Node IIfrom 116The next field, tree edges untouchedLevel by levelOne list per levelO(w)the page's BFS; children can be missing