JasonChang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

先遍历右子树

 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 }

 

posted on 2013-11-11 14:39  JasonChang  阅读(165)  评论(0编辑  收藏  举报