> For the complete documentation index, see [llms.txt](https://protegejj.gitbook.io/algorithm-practice/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://protegejj.gitbook.io/algorithm-practice/leetcode/tree.md).

# Tree

## 117. Populating Next Right Pointers in Each Node II

## 1. Question

Follow up for problem "Populating Next Right Pointers in Each Node".

What if the given tree could be any binary tree? Would your previous solution still work?

**Note:**

* You may only use constant extra space.

For example,\
Given the following binary tree,

```
         1
       /  \
      2    3
     / \    \
    4   5    7
```

After calling your function, the tree should look like:

```
         1 ->NULL
       /  \
      2 -> 3 ->NULL
     / \    \
    4-> 5 -> 7 ->NULL
```

## 2. Implementation

**(1) BFS**

```java
public class Solution {
    public void connect(TreeLinkNode root) {
        if (root == null) {
            return;
        }

        Queue<TreeLinkNode> queue = new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeLinkNode curNode = queue.remove();

                if (i == size - 1) {
                    curNode.next = null;
                }
                else {
                   curNode.next = queue.peek(); 
                }

                if (curNode.left != null) {
                    queue.add(curNode.left);
                }

                if (curNode.right != null) {
                    queue.add(curNode.right);
                }
            }
        }
    }
}
```

**(2) Iteration**

```java
public class Solution {
    public void connect(TreeLinkNode root) {
        TreeLinkNode preNode = null, curNode = root, leftMostNode = null;

        while (curNode != null) {
            while (curNode != null) {

                if (curNode.left != null) {
                    if (preNode != null) {
                        preNode.next = curNode.left;
                    }
                    else {
                        leftMostNode = curNode.left;
                    }
                    preNode = curNode.left;
                }

                if (curNode.right != null) {
                    if (preNode != null) {
                        preNode.next = curNode.right;
                    }
                    else {
                        leftMostNode = curNode.right;
                    }
                    preNode = curNode.right;
                }

                curNode = curNode.next;
            }
            curNode = leftMostNode;
            preNode = null;
            leftMostNode = null;
        }
    }
}
```

## 3. Time & Space Complexity

BFS: 时间复杂度: O(n), 空间复杂度: O(w), w是树中拥有最多node的一层中的node个数

Iteration: 时间复杂度: O(n), 空间复杂度: O(1)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://protegejj.gitbook.io/algorithm-practice/leetcode/tree.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
