Populating Next Right Pointers in Each Node II

先遍历右子树

 1 public class Solution {
 2     public void connect(TreeLinkNode root) {
 3         // IMPORTANT: Please reset any member data you declared, as
 4         // the same Solution instance will be reused for each test case.
 5         if(root == null)
 6             return;
 7             
 8         TreeLinkNode p = root.next;
 9         while(p != null){
10             if(p.left != null){
11                 p = p.left;
12                 break;
13             } 
14             if(p.right != null){
15                 p = p.right;
16                 break;
17             }
18             p = p.next;
19         }
20         
21         if(root.left != null)
22         {
23             if(root.right != null)
24                 root.left.next = root.right;
25             else
26             {
27                 root.left.next = p;
28             }
29         }
30         
31         if(root.right != null)
32         {
33             root.right.next = p;
34         }
35         if(root.right!=null)
36             connect(root.right);
37         if(root.left!=null)
38             connect(root.left);
39     }
40 }
原文地址:https://www.cnblogs.com/jasonC/p/3417896.html