# 填充每个节点的下一个右侧节点指针

题目链接: <https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii>

## 解题思路：

1. 采用`bfs`方式遍历每层节点
2. 用`queue`存放每层节点，逐个从头部出队，并将节点的`Next`指向`queue`中的第一个节点
3. 为了区分层别，需要用`size`记录每层节点数

```go
/**
 * Definition for a Node.
 * type Node struct {
 *     Val int
 *     Left *Node
 *     Right *Node
 *     Next *Node
 * }
 */

func connect(root *Node) *Node {
    if root==nil{
        return root
    }
    queue:=[]*Node{root}
    for len(queue)>0{
        size:=len(queue)
        for i:=0;i<size;i++{
            node:=queue[0]
            queue=queue[1:]
            if i==size-1{
                node.Next=nil
            }else{
                node.Next=queue[0]
            }
            if node.Left!=nil{
                queue=append(queue,node.Left)
            }
            if node.Right!=nil{
                queue=append(queue,node.Right)
            }
        }
    }
    return root
}
```

## 复杂度分析

* **时间复杂度：** 时间复杂度为$$O(n)$$，$$n$$为树的节点数
* **空间复杂度：** 空间复杂度为$$O(n)$$。


---

# 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://leetcodebook-1.gitbook.io/top-interview-150/tree/populating-next-right-pointers-in-each-node.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.
