116. Populating Next Right Pointers in Each Node && 117. Populating Next Right Pointers in Each Node II

116. Populating Next Right Pointers in Each Node

Given a binary tree

    struct TreeLinkNode {
      TreeLinkNode *left;
      TreeLinkNode *right;
      TreeLinkNode *next;
    }

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

Initially, all next pointers are set to NULL.

Note:

  • You may only use constant extra space.
  • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

 

For example,
Given the following perfect binary tree,

         1
       /  \
      2    3
     / \  / \
    4  5  6  7

 

After calling your function, the tree should look like:

         1 -> NULL
       /  \
      2 -> 3 -> NULL
     / \  / \
    4->5->6->7 -> NULL
Hide Tags
 Tree Depth-first Search
 
/**
 * Definition for binary tree with next pointer.
 * public class TreeLinkNode {
 *     int val;
 *     TreeLinkNode left, right, next;
 *     TreeLinkNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void connect(TreeLinkNode root) {
        if(root == null)
            return;
            
        if(root.left != null)
        {
            root.left.next = root.right;
            if(root.next != null)
                root.right.next = root.next.left;
        }
        connect(root.left);
        connect(root.right);
        
    }
}

 

117. 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
Hide Tags
 Tree Depth-first Search
 
O(n) BFS solution, no extra space used.
 
/**
 * Definition for binary tree with next pointer.
 * public class TreeLinkNode {
 *     int val;
 *     TreeLinkNode left, right, next;
 *     TreeLinkNode(int x) { val = x; }
 * }
 */
public class Solution {
    /* This method will connect all the children of all nodes that are at
     * the same level as root.
     * And then, find the first non-null child, and do the same thing for next level.
     */
  public void connect(TreeLinkNode root) {
    if (root == null)
      return;

    TreeLinkNode current = root;
    while (current != null) {
        //iterate through all the nodes at the same level as root
      if (current.left != null || current.right != null) { //if no children, we go to next node.
        TreeLinkNode currentFirstNextWithChildren = current.next;
        TreeLinkNode firstRightChild = null;
        //do the while loop to find the first non-null child of the right part.
        //By right part, I mean the branches to the right of root.
        while (firstRightChild == null && currentFirstNextWithChildren != null) {
          if (currentFirstNextWithChildren.left != null)
            firstRightChild = currentFirstNextWithChildren.left;
          else
            firstRightChild = currentFirstNextWithChildren.right;
          currentFirstNextWithChildren = currentFirstNextWithChildren.next;
        }

        //connect all the children nodes.
        if (current.left != null) {
          if (current.right != null) {
            current.left.next = current.right;
            current.right.next = firstRightChild;
          } else
            current.left.next = firstRightChild;
        } else
          current.right.next = firstRightChild;
      }
      current = current.next; //go to next node.
    }
    
    current = root;
    //Find the first non-null child, and do the same thing for next level.
    //The while loop is only for finding the first non-null child.
    while (current != null) {
      if (current.left != null) {
        connect(current.left);
        break;
      } else if (current.right != null) {
        connect(current.right);
        break;
      }
      current = current.next;
    }
  }
}

 

An even better solution:

/**
 * Definition for binary tree with next pointer.
 * public class TreeLinkNode {
 *     int val;
 *     TreeLinkNode left, right, next;
 *     TreeLinkNode(int x) { val = x; }
 * }
 */
public class Solution {
  public void connect(TreeLinkNode root) {
    TreeLinkNode head=root;//The left most node in the lower level
    TreeLinkNode prev=null;//The previous node in the lower level
    TreeLinkNode curr=null;//The current node in the upper level
    while (head!=null){
        curr=head;
        prev=null;
        head=null;
        while (curr!=null){
            if (curr.left!=null){
                if (prev!=null)
                    prev.next=curr.left;
                else 
                    head=curr.left;
                prev=curr.left;
            }
            if (curr.right!=null){
                if (prev!=null)
                    prev.next=curr.right;
                else 
                    head=curr.right;
                prev=curr.right;
            }
            curr=curr.next;
        }
    }
  }
}

 

 
 
posted @ 2016-07-12 13:32  新一代的天皇巨星  阅读(180)  评论(0编辑  收藏  举报