-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path116. Populating Next Right Pointers in Each Node
169 lines (128 loc) · 3.77 KB
/
116. Populating Next Right Pointers in Each Node
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
*/
class Solution {
public Node connect(Node root) {
if(root == null) return root;
Queue<Node> q = new LinkedList<>();
q.add(root);
while(!q.isEmpty()) {
int s = q.size();
List<Node> list = new ArrayList<>();
while(s-- > 0) {
Node cur = q.poll();
list.add(cur);
if(cur.left != null)
q.add(cur.left);
if(cur.right != null)
q.add(cur.right);
}
for(int i = 0; i < list.size() - 1; i++)
list.get(i).next = list.get(i + 1);
list.get(list.size() - 1).next = null;
}
return root;
}
}
/*
// Definition for a Node.
class Node {
public:
int val;
Node* left;
Node* right;
Node* next;
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
Node(int _val, Node* _left, Node* _right, Node* _next)
: val(_val), left(_left), right(_right), next(_next) {}
};
*/
class Solution {
public:
Node* connect(Node* root) {
if(not root) return root;
Node* head = root;
while(root and root->left) {
Node* cur = root;
while(cur) {
cur->left->next = cur->right;
cur->right->next = cur->next ? cur->next->left : nullptr;
cur = cur->next;
}
root = root->left;
}
return head;
}
};
#time O(n) space O(n) with freeing => end space O(1)
"""
# Definition for a Node.
class Node:
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
self.val = val
self.left = left
self.right = right
self.next = next
"""
class Solution:
def connect(self, root: 'Node') -> 'Node':
if not root:
return None
q = [root]
while q:
s = len(q)
for i, val in enumerate(q):
if i == s - 1:
val.next = None
else:
val.next = q[i + 1]
while s:
cur = q.pop(0)
if cur.left:
q.append(cur.left)
if cur.right:
q.append(cur.right)
s -= 1
return root
#time O(n) space O(1)
"""
# Definition for a Node.
class Node:
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
self.val = val
self.left = left
self.right = right
self.next = next
"""
class Solution:
def connect(self, root: 'Node') -> 'Node':
if not root:
return None
node = root
while node:
cur = node
while cur:
if cur.left:
cur.left.next = cur.right
if cur.right and cur.next:
cur.right.next = cur.next.left
cur = cur.next
node = node.left
return root