Question
Given a binary tree
struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
}
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.
Note:
- You may only use constant extra space.
- Recursive approach is fine, implicit stack space does not count as extra space for this problem.
Example:
Given the following perfect binary tree,
1
/ \
2 3
/ \ / \
4 5 6 7
After calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL
Solution
TODO
Code
/**
* Definition for binary tree with next pointer.
* public class TreeLinkNode {
* int val;
* TreeLinkNode left, right, next;
* TreeLinkNode(int x) { val = x; }
* }
*/
public class Solution {
public void connect(TreeLinkNode root) {
TreeLinkNode nextHead = root;
while(nextHead != null) {
TreeLinkNode curr = nextHead;
TreeLinkNode prevChild = null;
nextHead = null;
while(curr != null) {
if(curr.left != null) {
if(nextHead == null) nextHead = curr.left;
if(prevChild != null) {
prevChild.next = curr.left;
}
prevChild = curr.left;
}
if(curr.right != null) {
if(nextHead == null) nextHead = curr.right;
if(prevChild != null) {
prevChild.next = curr.right;
}
prevChild = curr.right;
}
curr = curr.next;
}
curr = nextHead;
}
}
}
Performance
O(num of nodes)
-
Previous
Problem: Binary Tree Zigzag Order Level Traversal -
Next
Problem: Verify Balanced Binary Tree