-
Notifications
You must be signed in to change notification settings - Fork 0
/
117-PopulatingNextRightPointersInEachNode2.cc
70 lines (69 loc) · 1.78 KB
/
117-PopulatingNextRightPointersInEachNode2.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
/*
树不一定是完美二叉树,而可为任意树
递归,时间复杂度O(n),空间复杂度O(1)
*/
class Solution
{
public:
void connect(TreeLinkNode *root)
{
if (root == nullptr)
return;
TreeLinkNode dummy(-1);
for (TreeLinkNode *cur = root, *prev = &dummy; cur; cur = cur->next)
{
if (cur->left != nullptr)
{
prev->next = cur->left;
prev = prev->next;
}
if (cur->right != nullptr)
{
prev->next = cur->right;
prev = prev->next;
}
}
connect(dummy.next);
}
};
/*
迭代,时间复杂度O(n),空间复杂度O(1)
*/
class Solution
{
public:
void connect(TreeLinkNode *root)
{
while (root)
{
TreeLinkNode *next = nullptr; // the first node of next level
TreeLinkNode *prev = nullptr; // previous node on the same level
for (; root; root = root->next)
{
if (!next)
next = root->left ? root->left : root->right;
if (root->left)
{
if (prev)
prev->next = root->left;
prev = root->left;
}
if (root->right)
{
if (prev)
prev->next = root->right;
prev = root->right;
}
}
root = next; // turn to next level
}
}
};