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

Leetcode | Populating Next Right Pointers in Each Node I & II

时间:2014-05-07 01:34:51      阅读:407      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   ext   

Populating Next Right Pointers in Each Node I

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

Method I

这里我用了bfs的方法,或者叫层次遍历。

bubuko.com,布布扣
 1 class Solution {
 2 public:
 3     void connect(TreeLinkNode *root) {
 4         if (root == NULL) {
 5             return;
 6         }
 7         queue<TreeLinkNode*> q;
 8         TreeLinkNode* p, *endOfLayer;
 9         q.push(root);
10         endOfLayer = root;
11         
12         while (!q.empty()) {
13             p = q.front();
14             q.pop();
15             
16             if (p->left) q.push(p->left);
17             if (p->right) q.push(p->right);
18             if (p == endOfLayer) {
19                 p->next = NULL; 
20                 endOfLayer = q.back();
21             } else {
22                 p->next = q.front();
23             }
24         }
25     }
26 };
bubuko.com,布布扣

Method II

网上的另一种方法是是利用了next这个指针。访问第k层时把k+1层的next指针设置好。每一层的开头用一个front指针表示,当前层当前结点用cur表示,下一层的前置结点用next表示。注意next为空时,表明是下一层的第一个结点,要特殊处理。 

因为是pefect binary tree,所以进入到下一层就是front = front->left;

这种方法的好处是仍然是层次遍历的思想,但是没有用到queue,用O(1)的空间。

bubuko.com,布布扣
 1 class Solution {
 2 public:
 3     void connect(TreeLinkNode *root) {
 4         if (root == NULL) {
 5             return;
 6         }
 7         
 8         TreeLinkNode *cur, *front, *next;
 9         front = cur = root;
10         next = NULL;
11         
12         while (front != NULL) {
13             if (cur->left != NULL) {
14                 if (next == NULL) {
15                     next = cur->left;
16                 } else {
17                     next->next = cur->left;
18                     next = next->next;
19                 }
20             }
21             if (cur->right != NULL) {
22                 if (next == NULL) {
23                     next = cur->right;
24                 } else {
25                     next->next = cur->right;
26                     next = next->next;
27                 }
28             }
29             cur = cur->next;
30             if (cur == NULL) {
31                 front = front->left;
32                 cur = front;
33                 next = NULL;
34             }
35         }
36     }
37 };
bubuko.com,布布扣

Method III

当然用递归也是可以的。也很直观。

bubuko.com,布布扣
class Solution {
public:
    void connect(TreeLinkNode *root) {
        if (root == NULL) {
            return;
        }
        
        if (root->left) {
            root->left->next = root->right;
        }
        if (root->right && root->next) {
            root->right->next = root->next->left;
        }
        connect(root->left);
        connect(root->right);
    }
};
bubuko.com,布布扣

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.

既然是要求O(1)空间,那么用queue(Method I)和递归(Method III)都不行了。

因为这里是任意的binary tree,所以要维护下一层的头指针。cur为空时会指下下一层的头指针,如果此时为空就退出。所以循环条件改为cur != NULL。

bubuko.com,布布扣
 1 class Solution {
 2 public:
 3     void connect(TreeLinkNode *root) {
 4         if (root == NULL) {
 5             return;
 6         }
 7         
 8         TreeLinkNode *cur, *nextFront, *next;
 9         cur = root;
10         nextFront = next = NULL;
11         
12         while (cur != NULL) {
13             if (cur->left != NULL) {
14                 if (next == NULL) {
15                     nextFront = next = cur->left;
16                 } else {
17                     next->next = cur->left;
18                     next = next->next;
19                 }
20             }
21             if (cur->right != NULL) {
22                 if (next == NULL) {
23                     nextFront = next = cur->right;
24                 } else {
25                     next->next = cur->right;
26                     next = next->next;
27                 }
28             }
29             cur = cur->next;
30             if (cur == NULL) {
31                 cur = nextFront;
32                 nextFront = next = NULL;
33             }
34         }
35     }
36 };
bubuko.com,布布扣

 

Leetcode | Populating Next Right Pointers in Each Node I & II,布布扣,bubuko.com

Leetcode | Populating Next Right Pointers in Each Node I & II

标签:style   blog   class   code   java   ext   

原文地址:http://www.cnblogs.com/linyx/p/3712334.html

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