-
Notifications
You must be signed in to change notification settings - Fork 118
/
117. Populating Next Right Pointers in Each Node II.cpp
127 lines (106 loc) · 4.18 KB
/
117. Populating Next Right Pointers in Each Node II.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//BFS with queue
//Runtime: 20 ms, faster than 52.71% of C++ online submissions for Populating Next Right Pointers in Each Node II.
//Memory Usage: 17.9 MB, less than 66.31% of C++ online submissions for Populating Next Right Pointers in Each Node II.
//time: O(N), space: O(N)
class Solution {
public:
Node* connect(Node* root) {
if(!root) return root;
queue<Node*> q;
q.push(root);
while(!q.empty()){
int level_size = q.size();
Node* prev = nullptr;
while(level_size-- > 0){
Node* cur = q.front(); q.pop();
if(prev) prev->next = cur;
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
prev = cur;
}
}
return root;
}
};
//O(1) space iterative solution
//https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/37811/Simple-solution-using-constant-space
//Runtime: 12 ms, faster than 96.85% of C++ online submissions for Populating Next Right Pointers in Each Node II.
//Memory Usage: 17.7 MB, less than 66.31% of C++ online submissions for Populating Next Right Pointers in Each Node II.
//time: O(N), space: O(1)
class Solution {
public:
Node* connect(Node* root) {
//first node of next level
Node *nhead = nullptr;
Node *prev = nullptr, *cur = root;
while(cur){
//process cur's next level
while(cur){
if(cur->left){
if(prev){
prev->next = cur->left;
}else{
nhead = cur->left;
}
prev = cur->left;
}
//else
if(cur->right){
if(prev){
prev->next = cur->right;
}else{
nhead = cur->right;
}
prev = cur->right;
}
//go to next node in current level
cur = cur->next;
}
//go to next level
cur = nhead;
prev = nhead = nullptr;
}
return root;
}
};
//O(1) space(not consider recursion stack) recursive solution
//https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/172861/Mostly-recursive-solution-O(n)-time-(beats-99.32)-and-O(1)-space-(without-considering-stack)
//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Populating Next Right Pointers in Each Node II.
//Memory Usage: 17.6 MB, less than 66.31% of C++ online submissions for Populating Next Right Pointers in Each Node II.
class Solution {
public:
Node* findnext(Node* node){
//recursively find the next leftmost child
if(!node) return node;
if(node->left) return node->left;
if(node->right) return node->right;
//current node is leaf, go to its right neighbor
return findnext(node->next);
}
Node* connect(Node* root) {
if(!root) return root;
if(root->left){
Node* target;
if(root->right){
target = root->right;
}else{
target = findnext(root->next);
}
root->left->next = target;
}
if(root->right){
root->right->next = findnext(root->next);
}
//https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/172861/Mostly-recursive-solution-O(n)-time-(beats-99.32)-and-O(1)-space-(without-considering-stack)/260137
/*
in the function "findnext",
we will recursively go right,
and it requires that the right subtree is connected first,
otherwise, therewill be a gap btw left and right subtree
consider: [2,1,3,0,7,9,1,2,null,1,0,null,null,8,8,null,null,null,null,7]
*/
connect(root->right);
connect(root->left);
return root;
}
};