-
Notifications
You must be signed in to change notification settings - Fork 52
/
Question41.java
51 lines (48 loc) · 1.06 KB
/
Question41.java
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
import java.util.ArrayDeque;
import java.util.ArrayList;
public class Solution {
public class TreeNode{
int val;
TreeNode left;
TreeNode right;
TreeNode(){}
TreeNode(int val){ this.val=val;}
TreeNode(int val, TreeNode left, TreeNode right){
this.val=val;
this.left=left;
this.right=right;
}
}
static TreeNode reverseOddLevels(TreeNode root) {
ArrayDeque<TreeNode> dq = new ArrayDeque<>();
dq.add(root);
int level=0;
while(dq.size()>0) {
int s = dq.size();
if(lvl%2==1) {
ArrayList <TreeNode> list = new ArrayList<>();
ArrayList <Integer> list1 = new ArrayList<>();
for(int i=0;i<s;i++) {
TreeNode p = dq.pop();
if(p.left!=null) dq.add(p.left);
if(p.right!=null) dq.add(p.right);
list.add(p);
list1.add(p.val);
}
for(int i=0,j=s-1;i<s;i++,j--) {
list.get(i).val=list1.get(j);
}
}
else
{
for(int i=0;i<s;i++) {
TreeNode p = dq.pop();
if(p.left!=null) dq.add(p.left);
if(p.right!=null) dq.add(p.right);
}
}
levrl++;
}
return root;
}
}