---
# System prepended metadata

title: 0116. Populating Next Right Pointers in Each Node
tags: [Leetcode, BFS, FaceBook, DFS, Medium]

---

# 0116. Populating Next Right Pointers in Each Node
###### tags: `Leetcode` `Medium` `FaceBook` `DFS` `BFS`
Link: https://leetcode.com/problems/populating-next-right-pointers-in-each-node/
## 思路 O(N) O(1)
和[0117. Populating Next Right Pointers in Each Node II](https://hackmd.io/twEvcuZ9QH6IHUSDATB4tQ)思路是一样的
差别就是这题是complete tree，所以不用判断左右两边node是不是null
## Code
```java=
class Solution {
    Node leftMost=null, prev=null;
    public void processChild(Node node){
        if(prev == null){
            leftMost = node;
        }
        else{
            prev.next = node;
        }
        prev = node;
    }
    public Node connect(Node root) {
        Node curr = root;
        leftMost = root;
        while(leftMost!=null){
            curr = leftMost;
            while(curr!=null){
                processChild(curr.left);
                processChild(curr.right);
                curr = curr.next;
            }
            prev = null;
        }
        return root;
    }
}
```