zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    论语言思维的差异
    lua经典问题
    跳槽的故事
    未来一年计划
    腾讯面试题 找重复的数
    nodejs学习
    node记录
    mysql 常用总结
    ubuntu 服务器搭建汇总
    ubuntu下安装golang
  • 原文地址:https://www.cnblogs.com/jasonC/p/3417896.html
Copyright © 2011-2022 走看看