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

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

时间: 2024-10-26 04:23:12

Leetcode | Populating Next Right Pointers in Each Node I & II的相关文章

leetcode - Populating Next Right Pointers in Each Node I&amp;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 nex

[leetcode] Populating Next Right Pointers in Each Node

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, al

[leetcode]Populating Next Right Pointers in Each Node @ Python

原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/ 题意: 1 / 2 3 / \ / 4 5 6 7变为: 1 -> NULL / 2 -> 3 -> NULL / \ / 4->5->6->7 -> NULL 解题思路:看到二叉树我们就想到需要使用递归的思路了.直接贴代码吧,思路不难. 代码: # Definition for a binary t

LeetCode——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 t

LeetCode: Populating Next Right Pointers in Each Node [116]

[题目] 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. Initiall

LeetCode: Populating Next Right Pointers in Each Node II [117]

[题目] 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 bin

leetcode -day14 Populating Next Right Pointers in Each Node I II

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

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

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

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

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