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

分析:

把每层的Node放在list里面,然后遍历list的每个点,设置它们的nextRight.

 1 /*
 2 // Definition for a Node.
 3 class Node {
 4     public int val;
 5     public Node left;
 6     public Node right;
 7     public Node next;
 8 
 9     public Node() {}
10     
11     public Node(int _val) {
12         val = _val;
13     }
14 
15     public Node(int _val, Node _left, Node _right, Node _next) {
16         val = _val;
17         left = _left;
18         right = _right;
19         next = _next;
20     }
21 };
22 */
23 
24 class Solution {
25     public Node connect(Node root) {
26         if (root == null) return null;
27         Queue<Node> queue = new LinkedList<>();
28 
29         queue.offer(root);
30 
31         while (!queue.isEmpty()) {
32             int size = queue.size();
33             Node prev = null;
34             for (int i = 0; i < size; i++) {
35                 Node current = queue.poll();
36                 if (prev != null) {
37                     prev.next = current;
38                 } 
39                 prev = current;
40                 if (current.left != null) {
41                     queue.offer(current.left);
42                 }
43                 if (current.right != null) {
44                     queue.offer(current.right);
45                 }
46             }
47         }
48         return root;
49     }
50 }

 

Another solution from Leetcode.

Here is a more elegant solution. The trick is to re-use the populated nextRight pointers. As mentioned earlier, we just need one more step for it to work. Before we passed the leftChild and rightChild to the recursion function itself, we connect the rightChild’s nextRight to the current node’s nextRight’s leftChild. In order for this to work, the current node’s nextRight pointer must be populated, which is true in this case.

 1 public class Solution {
 2     public void connect(TreeLinkNode p) {
 3         if (p == null) return;
 4         
 5         if (p.left != null) {
 6             p.left.next = p.right;
 7         }
 8       
 9         if (p.right != null) {
10             p.right.next = (p.next == null) ? null : p.next.left;
11         }
12         connect(p.left);
13         connect(p.right);
14     }
15 }

 

posted @ 2016-08-03 02:31  北叶青藤  阅读(287)  评论(0)    收藏  举报