Skip to content

Commit 298dfb2

Browse files
add accepted solution for 1104 and tests
1 parent 6ae4db4 commit 298dfb2

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

src/main/java/com/fishercoder/solutions/_1104.java

+54-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.fishercoder.solutions;
22

33
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.CommonUtils;
45
import com.fishercoder.common.utils.TreeUtils;
56

6-
import java.lang.reflect.Array;
77
import java.util.ArrayList;
8+
import java.util.Arrays;
89
import java.util.Collections;
910
import java.util.Deque;
1011
import java.util.LinkedList;
@@ -36,6 +37,7 @@ public static class Solution1 {
3637
/**This brute force solution is correct but results in TLE on LeetCode.*/
3738
public List<Integer> pathInZigZagTree(int label) {
3839
Deque<Integer> deque = buildZigZagOrderList(label);
40+
CommonUtils.printDeque(deque);
3941
TreeNode root = buildZigZagOrderTree(deque);
4042
TreeUtils.printBinaryTree(root);
4143
return dfs(root, label, new ArrayList<>());
@@ -97,4 +99,55 @@ private Deque<Integer> buildZigZagOrderList(int label) {
9799
return deque;
98100
}
99101
}
102+
103+
public static class Solution2 {
104+
/**We'll directly compute the index of its parent, it'll be much faster this way.*/
105+
public List<Integer> pathInZigZagTree(int label) {
106+
List<List<Integer>> lists = buildZigZagOrderList(label);
107+
CommonUtils.printListList(lists);
108+
List<Integer> result = new ArrayList<>();
109+
int index = findIndex(lists.get(lists.size() - 1), label);
110+
result.add(label);
111+
for (int i = lists.size() - 2; i >= 0; i--) {
112+
index /= 2;
113+
result.add(lists.get(i).get(index));
114+
}
115+
Collections.sort(result);
116+
return result;
117+
}
118+
119+
private int findIndex(List<Integer> level, int label) {
120+
for (int i = 0; i < level.size(); i++) {
121+
if (level.get(i) == label) {
122+
return i;
123+
}
124+
}
125+
return -1;
126+
}
127+
128+
private List<List<Integer>> buildZigZagOrderList(int label) {
129+
List<List<Integer>> lists = new ArrayList<>();
130+
int num = 1;
131+
int level = 2;
132+
lists.add(Arrays.asList(num));
133+
if (label == 1) {
134+
return lists;
135+
}
136+
List<Integer> newLevel = new ArrayList<>();
137+
do {
138+
newLevel.clear();
139+
num++;
140+
for (; num < Math.pow(2, level); num++) {
141+
newLevel.add(num);
142+
}
143+
num--;
144+
if (level % 2 == 0) {
145+
Collections.reverse(newLevel);
146+
}
147+
lists.add(new ArrayList<>(newLevel));
148+
level++;
149+
} while (newLevel.get(0) < label && newLevel.get(newLevel.size() - 1) < label);
150+
return lists;
151+
}
152+
}
100153
}

src/test/java/com/fishercoder/_1104Test.java

+20
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212

1313
public class _1104Test {
1414
private static _1104.Solution1 solution1;
15+
private static _1104.Solution2 solution2;
1516
private static List<Integer> expected;
1617

1718
@BeforeClass
1819
public static void setup() {
1920
solution1 = new _1104.Solution1();
21+
solution2 = new _1104.Solution2();
2022
}
2123

2224
@Test
@@ -81,4 +83,22 @@ public void test9() {
8183
assertEquals(expected, solution1.pathInZigZagTree(500000));
8284
}
8385

86+
@Test
87+
public void test10() {
88+
expected = Arrays.asList(1);
89+
assertEquals(expected, solution1.pathInZigZagTree(1));
90+
}
91+
92+
@Test
93+
public void test11() {
94+
expected = Arrays.asList(1, 3, 4, 14);
95+
assertEquals(expected, solution2.pathInZigZagTree(14));
96+
}
97+
98+
@Test
99+
public void test12() {
100+
expected = Arrays.asList(1);
101+
assertEquals(expected, solution2.pathInZigZagTree(1));
102+
}
103+
84104
}

0 commit comments

Comments
 (0)