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

Populating Next Right Pointers in Each Node

时间:2016-08-03 06:40:12      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

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 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 == null) return;
12         ArrayList<TreeLinkNode> list1 = new ArrayList<TreeLinkNode>();
13         ArrayList<TreeLinkNode> list2 = new ArrayList<TreeLinkNode>();
14         
15         list1.add(root);
16         
17         while (!list1.isEmpty()) {
18             for (int i = 0; i < list1.size(); i++) {
19                 if (list1.get(i).left != null) {
20                     list2.add(list1.get(i).left);
21                 }
22                 if (list1.get(i).right != null) {
23                     list2.add(list1.get(i).right);
24                 }              
25             }
26         for (int j = 0; j < list2.size() - 1; j++) {
27             list2.get(j).next = list2.get(j+1);
28         }
29             ArrayList<TreeLinkNode> temp = list1;
30             list1 = list2;
31             list2 = temp; 
32             list2.clear();
33         }
34     }
35 }

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 void connect(Node* p) {
 2   if (!p) return;
 3   if (p->leftChild)
 4   p->leftChild->nextRight = p->rightChild;
 5   if (p->rightChild)
 6     p->rightChild->nextRight = (p->nextRight) ?
 7                                p->nextRight->leftChild :
 8                                NULL;
 9   connect(p->leftChild);
10   connect(p->rightChild);
11 }

 

Populating Next Right Pointers in Each Node

标签:

原文地址:http://www.cnblogs.com/beiyeqingteng/p/5731340.html

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