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

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

时间:2014-10-21 21:06:24      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   使用   for   strong   sp   

Populating Next Right Pointers in Each Node Total
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

我们可以用递归处理左右子树.

 

 1 /* 试一下 recursion */
 2     public void connect(TreeLinkNode root) {
 3         if (root == null) {
 4             return;
 5         }
 6         
 7         rec(root);
 8     }
 9 
10     public void rec(TreeLinkNode root) {
11         if (root == null) {
12             return;
13         }
14 
15         if (root.left != null) {
16             root.left.next = root.right;            
17         }
18 
19         if (root.right != null) {
20             root.right.next = root.next.left;
21         }
22         
23         rec(root.left);
24         rec(root.right);
25     }

 

SOLUTION 2

用层次遍历也可以相当容易解出来,而且这种方法用在下一题一点不用变。

 但是 这个解法不能符合题意。题目要求我们使用 constant extra space.

 1 /*
 2      * 使用level traversal来做。
 3      * */
 4     public void connect1(TreeLinkNode root) {
 5         if (root == null) {
 6             return;
 7         }
 8         
 9         TreeLinkNode dummy = new TreeLinkNode(0);
10         Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>();
11         q.offer(root);
12         q.offer(dummy);
13         
14         while (!q.isEmpty()) {
15             TreeLinkNode cur = q.poll();
16             if (cur == dummy) {
17                 if (!q.isEmpty()) {
18                     q.offer(dummy);
19                 }
20                 continue;
21             }
22             
23             if (q.peek() == dummy) {
24                 cur.next = null;
25             } else {
26                 cur.next = q.peek();
27             }
28             
29             if (cur.left != null) {
30                 q.offer(cur.left);
31             }
32             
33             if (cur.right != null) {
34                 q.offer(cur.right);
35             }
36         }
37     }

 

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

标签:style   blog   color   io   ar   使用   for   strong   sp   

原文地址:http://www.cnblogs.com/yuzhangcmu/p/4041341.html

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