Skip to content

Commit 211ebb3

Browse files
authored
feat: add solutions to lc problem: No.2415 (#2085)
No.2415.Reverse Odd Levels of Binary Tree
1 parent 12cf12a commit 211ebb3

File tree

8 files changed

+187
-280
lines changed

8 files changed

+187
-280
lines changed

solution/2400-2499/2415.Reverse Odd Levels of Binary Tree/README.md

+35-68
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@
6565

6666
**方法一:BFS**
6767

68-
BFS 遍历二叉树,遍历到奇数层时,反转该层节点的值
68+
我们可以使用广度优先搜索的方法,用一个队列 $q$ 来存储每一层的节点,用一个变量 $i$ 记录当前层数。若 $i$ 为奇数,则将当前层的节点值反转
6969

70-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为二叉树的节点个数
70+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数
7171

7272
<!-- tabs:start -->
7373

@@ -87,20 +87,16 @@ class Solution:
8787
q = deque([root])
8888
i = 0
8989
while q:
90-
t = []
90+
if i & 1:
91+
l, r = 0, len(q) - 1
92+
while l < r:
93+
q[l].val, q[r].val = q[r].val, q[l].val
94+
l, r = l + 1, r - 1
9195
for _ in range(len(q)):
9296
node = q.popleft()
93-
if i & 1:
94-
t.append(node)
9597
if node.left:
9698
q.append(node.left)
97-
if node.right:
9899
q.append(node.right)
99-
if t:
100-
j, k = 0, len(t) - 1
101-
while j < k:
102-
t[j].val, t[k].val = t[k].val, t[j].val
103-
j, k = j + 1, k - 1
104100
i += 1
105101
return root
106102
```
@@ -129,30 +125,23 @@ class Solution {
129125
public TreeNode reverseOddLevels(TreeNode root) {
130126
Deque<TreeNode> q = new ArrayDeque<>();
131127
q.offer(root);
132-
int i = 0;
133-
while (!q.isEmpty()) {
128+
for (int i = 0; !q.isEmpty(); ++i) {
134129
List<TreeNode> t = new ArrayList<>();
135-
for (int n = q.size(); n > 0; --n) {
136-
TreeNode node = q.pollFirst();
130+
for (int k = q.size(); k > 0; --k) {
131+
var node = q.poll();
137132
if (i % 2 == 1) {
138133
t.add(node);
139134
}
140135
if (node.left != null) {
141136
q.offer(node.left);
142-
}
143-
if (node.right != null) {
144137
q.offer(node.right);
145138
}
146139
}
147-
if (!t.isEmpty()) {
148-
int j = 0, k = t.size() - 1;
149-
for (; j < k; ++j, --k) {
150-
int v = t.get(j).val;
151-
t.get(j).val = t.get(k).val;
152-
t.get(k).val = v;
153-
}
140+
for (int l = 0, r = t.size() - 1; l < r; ++l, --r) {
141+
var x = t.get(l).val;
142+
t.get(l).val = t.get(r).val;
143+
t.get(r).val = x;
154144
}
155-
++i;
156145
}
157146
return root;
158147
}
@@ -177,32 +166,22 @@ class Solution {
177166
public:
178167
TreeNode* reverseOddLevels(TreeNode* root) {
179168
queue<TreeNode*> q{{root}};
180-
int i = 0;
181-
vector<TreeNode*> t;
182-
while (!q.empty()) {
183-
t.clear();
184-
for (int n = q.size(); n; --n) {
169+
for (int i = 0; q.size(); ++i) {
170+
vector<TreeNode*> t;
171+
for (int k = q.size(); k; --k) {
185172
TreeNode* node = q.front();
186173
q.pop();
187174
if (i & 1) {
188175
t.push_back(node);
189176
}
190177
if (node->left) {
191178
q.push(node->left);
192-
}
193-
if (node->right) {
194179
q.push(node->right);
195180
}
196181
}
197-
if (t.size()) {
198-
int j = 0, k = t.size() - 1;
199-
for (; j < k; ++j, --k) {
200-
int v = t[j]->val;
201-
t[j]->val = t[k]->val;
202-
t[k]->val = v;
203-
}
182+
for (int l = 0, r = t.size() - 1; l < r; ++l, --r) {
183+
swap(t[l]->val, t[r]->val);
204184
}
205-
++i;
206185
}
207186
return root;
208187
}
@@ -222,31 +201,22 @@ public:
222201
*/
223202
func reverseOddLevels(root *TreeNode) *TreeNode {
224203
q := []*TreeNode{root}
225-
i := 0
226-
for len(q) > 0 {
204+
for i := 0; len(q) > 0; i++ {
227205
t := []*TreeNode{}
228-
for n := len(q); n > 0; n-- {
206+
for k := len(q); k > 0; k-- {
229207
node := q[0]
230208
q = q[1:]
231209
if i%2 == 1 {
232210
t = append(t, node)
233211
}
234212
if node.Left != nil {
235213
q = append(q, node.Left)
236-
}
237-
if node.Right != nil {
238214
q = append(q, node.Right)
239215
}
240216
}
241-
if len(t) > 0 {
242-
j, k := 0, len(t)-1
243-
for ; j < k; j, k = j+1, k-1 {
244-
v := t[j].Val
245-
t[j].Val = t[k].Val
246-
t[k].Val = v
247-
}
217+
for l, r := 0, len(t)-1; l < r; l, r = l+1, r-1 {
218+
t[l].Val, t[r].Val = t[r].Val, t[l].Val
248219
}
249-
i++
250220
}
251221
return root
252222
}
@@ -270,24 +240,21 @@ func reverseOddLevels(root *TreeNode) *TreeNode {
270240
*/
271241

272242
function reverseOddLevels(root: TreeNode | null): TreeNode | null {
273-
const queue = [root];
274-
let d = 0;
275-
while (queue.length !== 0) {
276-
const n = queue.length;
277-
const t: TreeNode[] = [];
278-
for (let i = 0; i < n; i++) {
279-
const node = queue.shift();
280-
if (d % 2 == 1) {
281-
t.push(node);
243+
const q: TreeNode[] = [root];
244+
for (let i = 0; q.length > 0; ++i) {
245+
if (i % 2) {
246+
for (let l = 0, r = q.length - 1; l < r; ++l, --r) {
247+
[q[l].val, q[r].val] = [q[r].val, q[l].val];
282248
}
283-
node.left && queue.push(node.left);
284-
node.right && queue.push(node.right);
285249
}
286-
const m = t.length;
287-
for (let i = 0; i < m >> 1; i++) {
288-
[t[i].val, t[m - 1 - i].val] = [t[m - 1 - i].val, t[i].val];
250+
const nq: TreeNode[] = [];
251+
for (const { left, right } of q) {
252+
if (left) {
253+
nq.push(left);
254+
nq.push(right);
255+
}
289256
}
290-
d++;
257+
q.splice(0, q.length, ...nq);
291258
}
292259
return root;
293260
}

solution/2400-2499/2415.Reverse Odd Levels of Binary Tree/README_EN.md

+39-66
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ The nodes at level 3 were 1, 1, 1, 1, 2, 2, 2, 2, and are 2, 2, 2, 2, 1, 1, 1, 1
5858

5959
## Solutions
6060

61+
**Solution 1: BFS**
62+
63+
We can use the Breadth-First Search (BFS) method, using a queue $q$ to store the nodes of each level, and a variable $i$ to record the current level. If $i$ is odd, we reverse the values of the nodes at the current level.
64+
65+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree.
66+
6167
<!-- tabs:start -->
6268

6369
### **Python3**
@@ -74,20 +80,16 @@ class Solution:
7480
q = deque([root])
7581
i = 0
7682
while q:
77-
t = []
83+
if i & 1:
84+
l, r = 0, len(q) - 1
85+
while l < r:
86+
q[l].val, q[r].val = q[r].val, q[l].val
87+
l, r = l + 1, r - 1
7888
for _ in range(len(q)):
7989
node = q.popleft()
80-
if i & 1:
81-
t.append(node)
8290
if node.left:
8391
q.append(node.left)
84-
if node.right:
8592
q.append(node.right)
86-
if t:
87-
j, k = 0, len(t) - 1
88-
while j < k:
89-
t[j].val, t[k].val = t[k].val, t[j].val
90-
j, k = j + 1, k - 1
9193
i += 1
9294
return root
9395
```
@@ -114,30 +116,23 @@ class Solution {
114116
public TreeNode reverseOddLevels(TreeNode root) {
115117
Deque<TreeNode> q = new ArrayDeque<>();
116118
q.offer(root);
117-
int i = 0;
118-
while (!q.isEmpty()) {
119+
for (int i = 0; !q.isEmpty(); ++i) {
119120
List<TreeNode> t = new ArrayList<>();
120-
for (int n = q.size(); n > 0; --n) {
121-
TreeNode node = q.pollFirst();
121+
for (int k = q.size(); k > 0; --k) {
122+
var node = q.poll();
122123
if (i % 2 == 1) {
123124
t.add(node);
124125
}
125126
if (node.left != null) {
126127
q.offer(node.left);
127-
}
128-
if (node.right != null) {
129128
q.offer(node.right);
130129
}
131130
}
132-
if (!t.isEmpty()) {
133-
int j = 0, k = t.size() - 1;
134-
for (; j < k; ++j, --k) {
135-
int v = t.get(j).val;
136-
t.get(j).val = t.get(k).val;
137-
t.get(k).val = v;
138-
}
131+
for (int l = 0, r = t.size() - 1; l < r; ++l, --r) {
132+
var x = t.get(l).val;
133+
t.get(l).val = t.get(r).val;
134+
t.get(r).val = x;
139135
}
140-
++i;
141136
}
142137
return root;
143138
}
@@ -162,32 +157,22 @@ class Solution {
162157
public:
163158
TreeNode* reverseOddLevels(TreeNode* root) {
164159
queue<TreeNode*> q{{root}};
165-
int i = 0;
166-
vector<TreeNode*> t;
167-
while (!q.empty()) {
168-
t.clear();
169-
for (int n = q.size(); n; --n) {
160+
for (int i = 0; q.size(); ++i) {
161+
vector<TreeNode*> t;
162+
for (int k = q.size(); k; --k) {
170163
TreeNode* node = q.front();
171164
q.pop();
172165
if (i & 1) {
173166
t.push_back(node);
174167
}
175168
if (node->left) {
176169
q.push(node->left);
177-
}
178-
if (node->right) {
179170
q.push(node->right);
180171
}
181172
}
182-
if (t.size()) {
183-
int j = 0, k = t.size() - 1;
184-
for (; j < k; ++j, --k) {
185-
int v = t[j]->val;
186-
t[j]->val = t[k]->val;
187-
t[k]->val = v;
188-
}
173+
for (int l = 0, r = t.size() - 1; l < r; ++l, --r) {
174+
swap(t[l]->val, t[r]->val);
189175
}
190-
++i;
191176
}
192177
return root;
193178
}
@@ -207,31 +192,22 @@ public:
207192
*/
208193
func reverseOddLevels(root *TreeNode) *TreeNode {
209194
q := []*TreeNode{root}
210-
i := 0
211-
for len(q) > 0 {
195+
for i := 0; len(q) > 0; i++ {
212196
t := []*TreeNode{}
213-
for n := len(q); n > 0; n-- {
197+
for k := len(q); k > 0; k-- {
214198
node := q[0]
215199
q = q[1:]
216200
if i%2 == 1 {
217201
t = append(t, node)
218202
}
219203
if node.Left != nil {
220204
q = append(q, node.Left)
221-
}
222-
if node.Right != nil {
223205
q = append(q, node.Right)
224206
}
225207
}
226-
if len(t) > 0 {
227-
j, k := 0, len(t)-1
228-
for ; j < k; j, k = j+1, k-1 {
229-
v := t[j].Val
230-
t[j].Val = t[k].Val
231-
t[k].Val = v
232-
}
208+
for l, r := 0, len(t)-1; l < r; l, r = l+1, r-1 {
209+
t[l].Val, t[r].Val = t[r].Val, t[l].Val
233210
}
234-
i++
235211
}
236212
return root
237213
}
@@ -255,24 +231,21 @@ func reverseOddLevels(root *TreeNode) *TreeNode {
255231
*/
256232

257233
function reverseOddLevels(root: TreeNode | null): TreeNode | null {
258-
const queue = [root];
259-
let d = 0;
260-
while (queue.length !== 0) {
261-
const n = queue.length;
262-
const t: TreeNode[] = [];
263-
for (let i = 0; i < n; i++) {
264-
const node = queue.shift();
265-
if (d % 2 == 1) {
266-
t.push(node);
234+
const q: TreeNode[] = [root];
235+
for (let i = 0; q.length > 0; ++i) {
236+
if (i % 2) {
237+
for (let l = 0, r = q.length - 1; l < r; ++l, --r) {
238+
[q[l].val, q[r].val] = [q[r].val, q[l].val];
267239
}
268-
node.left && queue.push(node.left);
269-
node.right && queue.push(node.right);
270240
}
271-
const m = t.length;
272-
for (let i = 0; i < m >> 1; i++) {
273-
[t[i].val, t[m - 1 - i].val] = [t[m - 1 - i].val, t[i].val];
241+
const nq: TreeNode[] = [];
242+
for (const { left, right } of q) {
243+
if (left) {
244+
nq.push(left);
245+
nq.push(right);
246+
}
274247
}
275-
d++;
248+
q.splice(0, q.length, ...nq);
276249
}
277250
return root;
278251
}

0 commit comments

Comments
 (0)