# 117. Populating Next Right Pointers in Each Node II

### Description

Given a binary tree

<div align="left"><img src="/files/-MG3RFSYfLuH38pYTtVf" alt=""></div>

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`.

### Constraints

* The number of nodes in the given tree is less than `6000`.
* `-100 <= node.val <= 100`

### Approach

### Links

* GeeksforGeeks
* [Leetcode](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/)
* [ProgramCreek](https://www.programcreek.com/2014/06/leetcode-populating-next-right-pointers-in-each-node-ii-java/)
* YouTube

### **Examples**

{% tabs %}
{% tab title="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.

![](/files/-MG3QDFgt01_B9Mt2gEw)
{% endtab %}
{% endtabs %}

### **Solutions**

{% tabs %}
{% tab title="Node" %}

```java
// 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;
    }
};
```

{% endtab %}

{% tab title="Solution 1" %}

```java
/**
 * Time complexity : 
 * Space complexity : 
 */

class Solution {
    public Node connect(Node root) {
        if(root == null) return null;
        
        if(root.left != null && root.right != null) {
            root.left.next = root.right;
            root.right.next = findNextNode(root.next);
        } else if(root.left != null) {
            root.left.next = findNextNode(root.next);
        } else if(root.right != null) {
            root.right.next = findNextNode(root.next);
        }
        // Right first
        connect(root.right);
        connect(root.left);
        
        return root;
    }
    
    private Node findNextNode(Node node) {
        if(node == null) return null;
        if(node.left != null) return node.left;
        if(node.right != null) return node.right;
        return findNextNode(node.next);
    }
}
```

{% endtab %}

{% tab title="Solution 2" %}

```java
/**
 * Time complexity : 
 * Space complexity : 
 */

class Solution {
    public Node connect(Node root) {
        Node head = root;
        Node prev = null;
        Node curr = null;
        while(head != null) {
            curr = head;
            prev = null;
            head = null;
            while(curr != null) {
                if(curr.left != null) {
                    if(prev == null) {
                        head = curr.left;
                    } else {
                        prev.next = curr.left;
                    }
                    prev = curr.left;
                }
                if(curr.right != null) {
                    if(prev == null) {
                        head = curr.right;
                    } else {
                        prev.next = curr.right;
                    }
                    prev = curr.right;
                }
                curr = curr.next;
            }
        }
        return root;
    }
}
```

{% endtab %}
{% endtabs %}

### **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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://code-snippets.hbamithkumara.com/leetcode/problems/101-200/populating-next-right-pointers-in-each-node-ii.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
