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     }
  • 相关阅读:
    ajax工作原理
    ajax 和xmlHttpRequest区别
    ajax 基本语法
    javascript 中XMLHttpRequest 实现前台向后台的交互
    Javascript 中ajax实现前台向后台交互
    javascript 中函数eval()
    两道有趣的面试题
    linux 中的快捷键
    linux awk命令详解
    linux sed命令详解
  • 原文地址:https://www.cnblogs.com/feiling/p/3268836.html
Copyright © 2011-2022 走看看