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

    Follow up for problem "Populating Next Right Pointers in Each Node".

    What if the given tree could be any binary tree? Would your previous solution still work?

    Note:

    • You may only use constant extra space.

     For example,

    Given the following binary tree,

             1
           /  
          2    3
         /     
        4   5    7
    

     After calling your function, the tree should look like:

             1 -> NULL
           /  
          2 -> 3 -> NULL
         /     
        4-> 5 -> 7 -> NULL

    [解题思路]

    对于任意的二叉树,上题的解不能解决问题,主要原因在于上题有效的next都是直接的sibling

    而本题有效的next肯能在不同的子树上,另外本题需要先递归处理右子树,再处理左子树 

    如下面的例子,如果先处理左子树,当处理到节点7的右子树节点0,我们发现节点7的next是9,但9没有子树,

    这时候我们应该去看9的next,但此时右子树并没有进行处理,9的next为null


      

     1 /**
     2  * Definition for binary tree with next pointer.
     3  * public class TreeLinkNode {
     4  *     int val;
     5  *     TreeLinkNode left, right, next;
     6  *     TreeLinkNode(int x) { val = x; }
     7  * }
     8  */
     9 public class Solution {
    10     public void connect(TreeLinkNode root) {
    11         // Start typing your Java solution below
    12         // DO NOT write main() function
    13         if(root == null){
    14             return;
    15         }
    16         if(root.left != null){
    17             if(root.right == null){
    18                 TreeLinkNode p = root.next;
    19                 boolean flag = true;
    20                 while(p != null && flag){
    21                     if(p.left != null){
    22                         root.left.next = p.left;
    23                         flag = false;
    24                     } else if(p.right != null){
    25                         root.left.next = p.right;
    26                         flag = false;
    27                     } else {
    28                         p = p.next;
    29                     }
    30                 }
    31                 
    32             } else{
    33                 root.left.next = root.right;
    34             }
    35         }
    36         if(root.right != null){
    37             if(root.next == null){
    38                 root.right.next = null;
    39             } else {
    40                 TreeLinkNode p = root.next;
    41                 boolean flag = true;
    42                 while(p != null && flag){
    43                     if(p.left != null){
    44                         root.right.next = p.left;
    45                         flag = false;
    46                     } else if(p.right != null){
    47                         root.right.next = p.right;
    48                         flag = false;
    49                     } else {
    50                         p = p.next;
    51                     }
    52                 }
    53             }
    54         }
    55         
    56         connect(root.right);
    57         connect(root.left);
    58     }
    59 }

     refactor code

     1 public void connect(TreeLinkNode root) {
     2         // Start typing your Java solution below
     3         // DO NOT write main() function
     4         if(root == null){
     5             return;
     6         }
     7         // find a valid next
     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             if(root.right == null){
    23                 root.left.next = p;                
    24             } else{
    25                 root.left.next = root.right;
    26             }
    27         }
    28         if(root.right != null){
    29             if(root.next == null){
    30                 root.right.next = null;
    31             } else {
    32                 root.right.next = p;
    33             }
    34         }
    35         
    36         connect(root.right);
    37         connect(root.left);
    38     }
  • 相关阅读:
    关于 相对论 的 一些 讨论推理 杂集
    反相 大全
    收录 猴哥 对于 相对论 水星进动 星光偏折 引力透镜 GPS 的 说法
    哲学 一词 起源于 古希腊 的 “爱智慧”
    字符流的父类
    字符编码
    对象流
    BufferStream 缓存流
    OutputStream 以及 使用文件输入输出流实现文件的复制操作
    InputStream
  • 原文地址:https://www.cnblogs.com/feiling/p/3268836.html
Copyright © 2011-2022 走看看