Populating Next Right Pointers in Each Node II LeetCode Solution

Minimum Cost to Merge Stones
Share:

Populating Next Right Pointers in Each Node II Given a binary tree

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:

Input: root = [1,2,3,4,5,null,7]
Output: [1,#,2,3,#,4,5,7,#]
Explanation: Given the above 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, 6000].
  • -100 <= Node.val <= 100

Populating Next Right Pointers in Each Node II Solutions

Time: O(n)
Space: O(1)

C++

class Solution {
 public:
  Node* connect(Node* root) {
    Node* node = root;  // the node just above current needling

    while (node) {
      Node dummy(0);  // dummy node before needling
      // needle children of node
      for (Node* needle = &dummy; node; node = node->next) {
        if (node->left) {  // needle left child
          needle->next = node->left;
          needle = needle->next;
        }
        if (node->right) {  // needle right child
          needle->next = node->right;
          needle = needle->next;
        }
      }
      node = dummy.next;  // move node to the next level
    }

    return root;
  }
};

Java

 class Solution {
  public Node connect(Node root) {
    Node node = root; // the node just above current needling

    while (node != null) {
      Node dummy = new Node(); // dummy node before needling
      // needle children of node
      for (Node needle = dummy; node != null; node = node.next) {
        if (node.left != null) { // needle left child
          needle.next = node.left;
          needle = needle.next;
        }
        if (node.right != null) { // needle right child
          needle.next = node.right;
          needle = needle.next;
        }
      }
      node = dummy.next; // move node to the next level
    }

    return root;
  }
}

Python

class Solution:
  def connect(self, root: 'Node') -> 'Node':
    node = root  # the node just above current needling

    while node:
      dummy = Node(0)  # dummy node before needling
      # needle children of node
      needle = dummy
      while node:
        if node.left:  # needle left child
          needle.next = node.left
          needle = needle.next
        if node.right:  # needle right child
          needle.next = node.right
          needle = needle.next
        node = node.next
      node = dummy.next  # move node to the next level

    return root

Watch Tutorial

Checkout more Solutions here

Leave a Comment

Your email address will not be published. Required fields are marked *

x