zoukankan      html  css  js  c++  java
  • 117. Populating Next Right Pointers in Each Node II

        /*
         * 117. Populating Next Right Pointers in Each Node II 
         * 2016-7-18 by Mingyang
         * 唯一的不同是每次要先找到一个第一个有效的next链接节点,并且递归的时候要先处理右子树,再处理左子树。
         * 主要的思路还是一样的,从上到下依次populate数据,唯一不同的是需要找到第一个孩子不为空的next节点
         * 这样的话才能找到,先进行root左边,再右边的找
         * 先右子树再左子树,重要的事情要说三遍!!!!!
         */
              public void connect1(TreeLinkNode root) {  
                    if (root == null) return;  
                    //如果右孩子不为空,左孩子的next是右孩子。  
                    //反之,找root next的至少有一个孩子不为空的节点  
                    if (root.left != null) {  
                        if (root.right != null) {  
                            root.left.next = root.right;  
                        }  
                        else {  
                            TreeLinkNode p = root.next;  
                            while (p != null && p.left == null && p.right == null)  
                                p = p.next;  
                            if (p != null)  
                                root.left.next = p.left == null ? p.right : p.left;  
                        }  
                    }  
                    //右孩子的next 根节点至少有一个孩子不为空的next  
                    if (root.right != null) {  
                        TreeLinkNode p = root.next;  
                        while (p != null && p.left == null && p.right == null)  
                            p = p.next;  
                        if (p != null)  
                            root.right.next = p.left == null ? p.right : p.left;  
                    }  
                    connect1(root.right);      
                    connect1(root.left);  
               } 
              //自己的代码,写的不错。思路一样
              public void connect3(TreeLinkNode root) {
                  if(root==null)
                    return;
                  if(root.right!=null){
                      TreeLinkNode p=root.next;
                      while(p!=null){
                         if(p.left==null&&p.right==null)
                           p=p.next;
                         else{
                            root.right.next=p.left==null?p.right:p.left;
                            break;
                         }
                      }
                  }
                  if(root.left!=null){
                      if(root.right!=null){
                          root.left.next=root.right;
                      }else{
                        TreeLinkNode p=root.next;
                      while(p!=null){
                         if(p.left==null&&p.right==null)
                           p=p.next;
                         else{
                           root.left.next=p.left==null?p.right:p.left;
                           break;
                         }
                      }
                    } 
                  }
                  connect3(root.right);
                  connect3(root.left);
                  
              }
  • 相关阅读:
    This project references NuGet package(s) that are missing on this computer
    Industry Engineer
    winform
    OPC
    groupbox 下的datagridview的列标题字体修改混乱
    vs远程调试
    nuget pack 时不包含依赖包(而不是引用项目的dll,区别于IncludeReferencedProjects)
    Inno Setup入门(二十二)——Inno Setup类参考(8)
    学习GitHub
    python3.4读取excel数据绘图
  • 原文地址:https://www.cnblogs.com/zmyvszk/p/5511526.html
Copyright © 2011-2022 走看看