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

Populating Next Right Pointers in Each Node 解答

时间:2015-09-29 11:09:26      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

Question

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

Solution 1 -- BFS

Key to the solution is to traverse tree level by level. Therefore, we can use BFS. Time complexity O(n), n is the number of nodes, and space cost is O(n).

 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         // We can use BFS to solve this problem
12         List<TreeLinkNode> current = new ArrayList<TreeLinkNode>();
13         List<TreeLinkNode> next;
14         if (root == null)
15             return;
16         current.add(root);
17         while (current.size() > 0) {
18             next = new ArrayList<TreeLinkNode>();
19             int length = current.size();
20             for (int i = 0; i < length; i++) {
21                 TreeLinkNode currentTreeNode = current.get(i);
22                 if (i < length - 1)
23                     currentTreeNode.next = current.get(i + 1);
24                 else
25                     currentTreeNode.next = null;
26                 if (currentTreeNode.left != null)
27                     next.add(currentTreeNode.left);
28                 if (currentTreeNode.right != null)
29                     next.add(currentTreeNode.right);
30             }
31             current = next;
32         }
33     }
34 }

Solution 2 -- Recursive

Consider for one node, the connection is done when:

1. Its left node (if exists) connect to its right node.

2. Its right node (if exists) connect to its next node‘s left child.

3. Its left child and right child are all connected.

Note the prerequisite for this problem is perfect binary tree (every parent has two children). Time complexity O(n), space cost O(1).

 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)
12             return;
13         if (root.left != null)
14             root.left.next = root.right;
15         if (root.right != null && root.next != null)
16             root.right.next = root.next.left;
17         connect(root.left);
18         connect(root.right);
19     }
20 }

Solution 3 -- Four Pointers

We can use four pointers to traverse the tree.

(referrence: ProgramCreek)

技术分享

 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)
12             return;
13         TreeLinkNode lastHead = root;//prevous level‘s head 
14         TreeLinkNode lastCurrent = null;//previous level‘s pointer
15         TreeLinkNode currentHead = null;//current level‘s head
16         TreeLinkNode current = null;//current level‘s pointer
17         
18         while (lastHead != null) {
19             lastCurrent = lastHead;
20             currentHead = lastHead.left;
21             current = lastCurrent.left;
22             while (lastCurrent != null && current != null) {
23                 current.next = lastCurrent.right;
24                 current = current.next;
25                 lastCurrent = lastCurrent.next;
26                 if (lastCurrent != null) {
27                     current.next = lastCurrent.left;
28                     current = current.next;
29                 }
30             }
31             lastHead = currentHead;
32             currentHead = null;
33         }
34     }
35 }

 

Populating Next Right Pointers in Each Node 解答

标签:

原文地址:http://www.cnblogs.com/ireneyanglan/p/4845698.html

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