码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode--Populating Next Right Pointers in Each Node

时间:2014-06-05 13:42:24      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:des   c   class   blog   code   java   

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
 * Definition for binary tree with next pointer.
 * public class TreeLinkNode {
 *     int val;
 *     TreeLinkNode left, right, next;
 *     TreeLinkNode(int x) { val = x; }
 * }
 */
public class Solution {
    /**To solve this problem, we use a queue to save the nodes in  the same level.<br>
     *
     * @param root -- The root node of the input tree.
     * @author Averill Zheng
     * @version 2014-06-03
     * @since JDK 1.7
     */
    public void connect(TreeLinkNode root) {
        if(root != null){
            Queue<TreeLinkNode> node = new LinkedList<TreeLinkNode>();
            node.add(root);
            Queue<TreeLinkNode> nextLevel = new LinkedList<TreeLinkNode>();
            while(node.peek() != null){
                 
                TreeLinkNode first = node.poll();
                if(first.left != null)
                    nextLevel.add(first.left);
                if(first.right != null)
                    nextLevel.add(first.right);
                 
                while(node.peek() != null){
                    TreeLinkNode aNode = node.poll();
                    if(aNode.left != null)
                        nextLevel.add(aNode.left);
                    if(aNode.right != null)
                        nextLevel.add(aNode.right);
                    first.next = aNode;
                    first = first.next;
                }
                first.next = null;
                node = nextLevel;
                nextLevel = new LinkedList<TreeLinkNode>();
            }
        }
    }
}

 

The following code uses constant extra memory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Solution {
    public void connect(TreeLinkNode root) {
        if(root != null){
            root.next = null;
            TreeLinkNode topLevel = root;
            TreeLinkNode nextLevelHead = null;
            TreeLinkNode node = null;
            while(topLevel != null){
                if(topLevel.left != null){
                    nextLevelHead = topLevel.left;
                    node = nextLevelHead;
                    node.next = topLevel.right;
                    node = node.next;
                }
                topLevel = topLevel.next;
                while(topLevel != null){
                    if(topLevel.left != null && node != null){
                        node.next = topLevel.left;
                        node = node.next;
                        node.next = topLevel.right;
                        node = node.next;
                    }
                    topLevel = topLevel.next;
                }
                if(node != null){
                    node.next = null;
                    node = node.next;
                }
                topLevel = nextLevelHead;
                nextLevelHead = null;
            }
        }
    }
}

    

leetcode--Populating Next Right Pointers in Each Node,布布扣,bubuko.com

leetcode--Populating Next Right Pointers in Each Node

标签:des   c   class   blog   code   java   

原文地址:http://www.cnblogs.com/averillzheng/p/3769124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!