# 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; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up