首页 > 其他 > 详细

LeetCode: Populating Next Right Pointers in Each Node II 解题报告

时间:2014-10-21 21:07:42      阅读:204      评论:0      收藏:0      [点我收藏+]

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

SOLUTION 1

本题还是可以用Level Traversal 轻松解出,连代码都可以跟上一个题目一模一样。Populating Next Right Pointers in Each Node Total

但是不符合空间复杂度的要求:constant extra space.

bubuko.com,布布扣
 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         }
14         
15         TreeLinkNode dummy = new TreeLinkNode(0);
16         Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>();
17         
18         q.offer(root);
19         q.offer(dummy);
20         
21         while(!q.isEmpty()) {
22             TreeLinkNode cur = q.poll();
23             if (cur == dummy) {
24                 if (!q.isEmpty()) {
25                     q.offer(dummy);
26                 }
27                 continue;
28             }
29             
30             if (q.peek() == dummy) {
31                 cur.next = null;
32             } else {
33                 cur.next = q.peek();    
34             }
35             
36             if (cur.left != null) {
37                 q.offer(cur.left);
38             }
39             
40             if (cur.right != null) {
41                 q.offer(cur.right);
42             }
43         }
44         
45     }
46 }
View Code

SOLUTION 2

 我们可以用递归解出。注意从右往左加next。否则的话 右边未建立,左边你没找不到next. 这个算符合constant extra space.

bubuko.com,布布扣
 1 public void connect(TreeLinkNode root) {
 2         if (root == null) {
 3             return;
 4         }
 5         
 6         TreeLinkNode cur = root.next;
 7         TreeLinkNode next = null;
 8         // this is very important. should exit after found the next.
 9         while (cur != null && next == null) {
10             if (cur.left != null) {
11                 next = cur.left;
12             } else if (cur.right != null) {
13                 next = cur.right;
14             } else {
15                 cur = cur.next;
16             }
17         }
18         
19         if (root.right != null) {
20             root.right.next = next;
21             next = root.right;
22         }
23         
24         if (root.left != null) {
25             root.left.next = next;
26         }
27         
28         // The order is very important. We should deal with right first!
29         connect(root.right);
30         connect(root.left);
31     }
View Code

 

LeetCode: Populating Next Right Pointers in Each Node II 解题报告

原文:http://www.cnblogs.com/yuzhangcmu/p/4041345.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!