-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathReverseOddLevelsOfBinaryTree.java
122 lines (113 loc) · 3.44 KB
/
ReverseOddLevelsOfBinaryTree.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
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
/*https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/*/
/**
* Definition for a binary tree node.
* 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;
* }
* }
*/
class Solution {
public TreeNode reverseOddLevels(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
Queue<TreeNode> queue2 = new LinkedList<TreeNode>();
boolean flag = false;
queue.add(root);
while (!queue.isEmpty())
{
int len = queue.size();
if (flag)
{
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < len; ++i)
{
TreeNode node = queue.poll();
list.add(node.val);
queue2.add(node);
}
for (int i = len-1; i >= 0; --i)
{
TreeNode node = queue2.poll();
node.val = list.get(i);
queue.add(node);
}
}
for (int i = 0; i < len; ++i)
{
TreeNode node = queue.poll();
if (node.left != null) queue.add(node.left);
if (node.right != null) queue.add(node.right);
}
flag = !flag;
}
return root;
}
}
class Solution {
public TreeNode reverseOddLevels(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
int level = 0;
while (!queue.isEmpty()) {
List<TreeNode> list = new ArrayList<>();
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
list.add(node);
if (node.left != null) queue.add(node.left);
if (node.right != null) queue.add(node.right);
}
if (level % 2 == 1) {
int i = 0, j = size - 1;
while (i < j) {
int temp = list.get(i).val;
list.get(i).val = list.get(j).val;
list.get(j).val = temp;
i++;
j--;
}
}
level++;
}
return root;
}
}
//legendary recursion
/**
* Definition for a binary tree node.
* 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;
* }
* }
*/
class Solution {
public TreeNode reverseOddLevels(TreeNode root) {
reverse(root, root, root.left, root.right, true);
return root;
}
private void reverse(TreeNode parentL, TreeNode parentR, TreeNode left, TreeNode right, boolean isOdd){
if (left == null) return;
if (isOdd){
int temp = left.val;
left.val = right.val;
right.val = temp;
}
reverse(left, right, left.left, right.right, !isOdd);
reverse(left, right, left.right, right.left, !isOdd);
}
}