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

本文介绍了三种方法来填充完美二叉树中每个节点的下一个指针,包括使用队列的层次遍历方法、利用next指针的层次遍历方法以及递归方法,重点讨论了在任意二叉树情况下如何解决此问题。

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的代码是一致的。

转载于:https://www.cnblogs.com/linyx/p/3712334.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值