Skip to main content

Populating Next Right Pointers in Each Node


Populating Next Right Pointers in Each Node: 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:

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.

Example 1:

image

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.

Example 2:
Input: root = []
Output: []

Constraints:
  • The number of nodes in the tree is in the range [0, 2^12 - 1].
  • -1000 <= Node.val <= 1000

Try this Problem on your own or check similar problems:

  1. Populating Next Right Pointers in Each Node II
  2. Binary Tree Right Side View
  3. Cycle Length Queries in a Tree
Solution:
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node next;

public Node() {}

public Node(int _val) {
val = _val;
}

public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
*/

class Solution {
public Node connect(Node root) {
if(root == null) return null;
Node treeIter = root;

while(treeIter != null){
Node levelIter = treeIter;
while(levelIter != null && levelIter.left != null){
levelIter.left.next = levelIter.right;
if(levelIter.next != null){
levelIter.right.next = levelIter.next.left;
}
levelIter = levelIter.next;
}
treeIter = treeIter.left;
}

return root;
}
}

Time/Space Complexity:
  • Time Complexity: O(n)
  • Space Complexity: O(1)

Explanation:

It's like tree level traversal, so we keep two iterators treeIter which iterates over the tree starting with the root and going to the leaf nodes (so it's always pointing to the current level) and then levelIter which actually traverses through the nodes of the current level treeIter is pointing to. For each level we start by positioning the treeIter to the leftmost node treeIter = treeIter.left;. While we're traversing the level with levelIter we first make the connection between the left and right children of the current level node, and then we check if the current node has a right sibling, if it does we have to make a connection between its right child and its right sibling's left child (levelIter.right.next = levelIter.next.left;). We move the pointer to its right sibling (if there is any), moving to the next node in the current level. Finally, we return the root of the tree as final result. We don't use any additional space so space complexity is O(1), and we iterate over all nodes in the tree which makes the time complexity O(n).