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

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的方法,或者叫层次遍历。

 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 };

Method II

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

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

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

 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 };

Method III

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

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);
    }
};

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。

 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 };

 第三次刷的时候,代码简洁许多了。

 1 void connect(TreeLinkNode *root) {
 2         TreeLinkNode h(0), *p = root, *next;
 3         
 4         while (p) {
 5             next = &h;
 6             h.next = NULL;
 7             for (; p; p = p->next) {
 8                 if (p->left) {
 9                     next->next = p->left;
10                     next = next->next;
11                 } 
12                 if (p->right) {
13                     next->next = p->right;
14                     next = next->next;
15                 }
16             }
17             p = h.next;
18         }
19     }

I和II的代码是一致的。

posted @ 2014-05-06 19:56  linyx  阅读(161)  评论(0编辑  收藏  举报