LeetCode - Populating Next Right Pointers in Each Node

2 minute read

Problem description

description

You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:

1
2
3
4
5
6
struct Node {
  int val;
  Node *left;
  Node *right;
  Node *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.

Follow up:

  • You may only use constant extra space.
  • Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.

populating-next-right-pointers-in-each-node

1
2
3
Input: root = [1,2,3,4,5,6,7]
Output: [1,#,2,3,#,4,5,6,7,#]
Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.

Constraints:

  • The number of nodes in the given tree is less than 4096.
  • -1000 <= node.val <= 1000

Analysis

The most easiest way to solve this is to use BFS.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public Node connect(Node root) {
        if (root == null){
            return root;
        }
        Queue<Node> q = new ArrayDeque<>();
        
        q.offer(root);
        
        while(!q.isEmpty()){
            List<Node> list = new ArrayList<>();
            int size = q.size();
            
            for (int i = 0; i< size;i++){
                Node node =q.poll();
                list.add(node);
                
                if (node.left != null){
                    q.offer(node.left);
                }
                
                if (node.right != null){
                    q.offer(node.right);
                }
            }
            
            for (int i = 0; i < size - 1; i++){
                Node node = list.get(i);
                Node next = list.get(i+1);
                node.next = next;
            }
        }
        
        return root;
    }

However, if we want to do the follow-up with O(1) SC, then we can use recursion to do this.

The point is if we have a connected level, we can easily get the next level connected.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
    public Node connect(Node root) {
        
        if (root == null) {
            return root;
        }
        
        // Start with the root node. There are no next pointers
        // that need to be set up on the first level
        Node leftmost = root;
        
        // Once we reach the final level, we are done
        while (leftmost.left != null) {
            
            // Iterate the "linked list" starting from the head
            // node and using the next pointers, establish the 
            // corresponding links for the next level
            Node head = leftmost;
            
            while (head != null) {
                
                // CONNECTION 1
                head.left.next = head.right;
                
                // CONNECTION 2
                if (head.next != null) {
                    head.right.next = head.next.left;
                }
                
                // Progress along the list (nodes on the current level)
                head = head.next;
            }
            
            // Move onto the next level
            leftmost = leftmost.left;
        }
        
        return root;
    }
}

What to improve

  • input is null