leetcode--Populating Next Right Pointers in Each Node II

来源:互联网 发布:iis url重写 端口号 编辑:程序博客网 时间:2024/06/05 03:01

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指向其右边的相邻节点,如果右边没有节点,则指向Null。该树为任意二叉树。


解法1:层次遍历。使用两个栈来交替记录两层。对于每个层,逐一连接即可。

[java] view plain copy
  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.         if(root==nullreturn;    
  12.         Stack<TreeLinkNode> stack1 = new Stack<TreeLinkNode>();    
  13.         List<TreeLinkNode> stack2 = new ArrayList<TreeLinkNode>();    
  14.         stack1.add(root);    
  15.         while(stack1.size()>0||stack2.size()>0){              
  16.             TreeLinkNode pre = null;    
  17.             while(stack1.size()>0){    
  18.                 TreeLinkNode t = stack1.pop();          
  19.                 if(t.left!=null) stack2.add(t.left);    
  20.                 if(t.right!=null) stack2.add(t.right);    
  21.                 if(pre!=null){    
  22.                     pre.next = t;                       
  23.                 }    
  24.                 pre = t;    
  25.             }    
  26.             Collections.reverse(stack2);    
  27.             stack1.addAll(stack2);    
  28.             stack2.clear();    
  29.         }    
  30.     }  
  31. }  

解法2:层次遍历,和Populating Next Right Pointers in Each Node里面的解法类似,主要问题是,1,找到下一层开始的第一个节点;2,找到当前节点右边的第一个节点

[java] view plain copy
  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.         if(root==nullreturn;  
  12.         TreeLinkNode cur = root;//上一层第一个节点  
  13.         TreeLinkNode start = null;  
  14.         root.next = null;  
  15.         while(root!=null){//没有到最后一层的第一个节点  
  16.             start = findStart(root);//当前层第一个节点             
  17.             cur = start;              
  18.             while(cur!=null){//遍历当前层  
  19.                cur.next = findNext(root,cur);  
  20.                cur = cur.next;  
  21.             }             
  22.             root = start;//当前层第一个节点  
  23.         }  
  24.     }  
  25.       
  26.     public TreeLinkNode findStart(TreeLinkNode root){//找到下一层的第一个节点  
  27.         while(root!=null){  
  28.             if(root.left!=nullreturn root.left;  
  29.             if(root.right!=nullreturn root.right;  
  30.             root = root.next;  
  31.         }  
  32.         return null;  
  33.     }  
  34.       
  35.     public TreeLinkNode findNext(TreeLinkNode root,TreeLinkNode cur){//找到当前节点右边的第一个节点  
  36.         while(root!=null){//找到当前节点父节点  
  37.             if(root.left==cur||root.right==cur) break;  
  38.             root = root.next;             
  39.         }         
  40.         if(root.right==cur) root=root.next;//如果当前节点在父节点的右边,将root指向它的右边   
  41.         while(root!=null){  
  42.             if(root.left!=cur&&root.left!=nullreturn root.left;  
  43.             if(root.right!=cur&&root.right!=nullreturn root.right;  
  44.             root = root.next;  
  45.         }  
  46.         return null;  
  47.     }  
  48. }  

解法3:层次遍历。关键在于保留下一层的第一个节点。另外保留一个pre指向前一个节点。
[java] view plain copy
  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.         if(root==nullreturn;  
  12.         TreeLinkNode cur = root;//上一层第一个节点  
  13.         while(root!=null){//没有到最后一层的第一个节点  
  14.             cur = root;  
  15.             TreeLinkNode temp = new TreeLinkNode(-1);//该节点用于保存下一层的第一个节点             
  16.             TreeLinkNode pre = temp;  
  17.             while(cur!=null){//遍历当前层  
  18.                if(cur.left!=null){  
  19.                    pre.next = cur.left;  
  20.                    pre = pre.next;  
  21.                }  
  22.                if(cur.right!=null){  
  23.                    pre.next = cur.right;  
  24.                    pre = pre.next;  
  25.                }  
  26.                cur = cur.next;  
  27.             }             
  28.             root = temp.next;//下一层的第一个节点  
  29.         }  
  30.     }  
  31. }  

解法4:将解法3的迭代换成递归

[java] view plain copy
  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.         if(root==nullreturn;  
  12.         TreeLinkNode temp = new TreeLinkNode(-1);             
  13.         TreeLinkNode pre = temp;  
  14.         while(root!=null){//遍历当前层  
  15.            if(root.left!=null){  
  16.                pre.next = root.left;  
  17.                pre = pre.next;  
  18.            }  
  19.            if(root.right!=null){  
  20.                pre.next = root.right;  
  21.                pre = pre.next;  
  22.            }  
  23.            root = root.next;  
  24.         }             
  25.         connect(temp.next);//连接下一层  
  26.     }  

原文链接http://blog.csdn.net/crazy__chen/article/details/46593639

阅读全文
0 0