Thursday, January 15, 2015

Populating Next Right Pointers in Each Node II (LeetCode Tree)

Question: 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

Idea: Both depth-first search and breadth-first search work with the same time complexity. However, depth-first search is more space efficient. For depth-first search, always keeps track of the previous node to jump through the missing leaves. For breadth-first search, use a queue to remember each row.

Time: O(n) Space: O(1)

Code:
 public class Solution {  
   public void connect(TreeLinkNode root) {  
     if(root==null)  
       return;  
     TreeLinkNode parent=root;  
     TreeLinkNode pre;  
     TreeLinkNode next;  
     while(parent!=null)  
     {  
       pre=null;  
       next=null;  
       while(parent!=null)  
       {  
         if(next==null)  
           next=(parent.left!=null)?parent.left:parent.right;  
         if(parent.left!=null)  
         {  
           if(pre!=null)  
           {  
             pre.next=parent.left;  
             pre=pre.next;  
           }  
           else  
             pre=parent.left;  
         }  
         if(parent.right!=null)  
         {  
           if(pre!=null)  
           {  
             pre.next=parent.right;  
             pre=pre.next;  
           }  
           else  
             pre=parent.right;  
         }  
         parent=parent.next;  
       }  
       parent=next;  
     }  
   }  
 }  

No comments:

Post a Comment