Skip to content

Commit 839dcdb

Browse files
refactor 199
1 parent 8a3d4d7 commit 839dcdb

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

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

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,52 @@
2424

2525
public class _199 {
2626

27-
public List<Integer> rightSideView(TreeNode root) {
28-
List<Integer> result = new ArrayList<>();
29-
if (root == null) {
27+
public static class Solution1 {
28+
/**credit: https://leetcode.com/problems/binary-tree-right-side-view/discuss/56012/My-simple-accepted-solution(JAVA)*/
29+
public List<Integer> rightSideView(TreeNode root) {
30+
List<Integer> result = new ArrayList<>();
31+
rightView(root, result, 0);
3032
return result;
3133
}
32-
Queue<TreeNode> q = new LinkedList<>();
33-
q.offer(root);
34-
while (!q.isEmpty()) {
35-
int size = q.size();
36-
for (int i = 0; i < size; i++) {
37-
TreeNode curr = q.poll();
38-
if (i == size - 1) {
39-
result.add(curr.val);
40-
}
41-
if (curr.left != null) {
42-
q.offer(curr.left);
43-
}
44-
if (curr.right != null) {
45-
q.offer(curr.right);
34+
35+
void rightView(TreeNode curr, List<Integer> result, int currDepth) {
36+
if (curr == null) {
37+
return;
38+
}
39+
if (currDepth == result.size()) {
40+
result.add(curr.val);
41+
}
42+
rightView(curr.right, result, currDepth + 1);
43+
rightView(curr.left, result, currDepth + 1);
44+
}
45+
}
46+
47+
public static class Solution2 {
48+
/**BFS the tree*/
49+
public List<Integer> rightSideView(TreeNode root) {
50+
List<Integer> result = new ArrayList<>();
51+
if (root == null) {
52+
return result;
53+
}
54+
Queue<TreeNode> q = new LinkedList<>();
55+
q.offer(root);
56+
while (!q.isEmpty()) {
57+
int size = q.size();
58+
for (int i = 0; i < size; i++) {
59+
TreeNode curr = q.poll();
60+
if (i == size - 1) {
61+
result.add(curr.val);
62+
}
63+
if (curr.left != null) {
64+
q.offer(curr.left);
65+
}
66+
if (curr.right != null) {
67+
q.offer(curr.right);
68+
}
4669
}
4770
}
71+
return result;
4872
}
49-
return result;
5073
}
5174

5275
}

src/test/java/com/fishercoder/_199Test.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,21 @@
1111
import static org.junit.Assert.assertEquals;
1212

1313
public class _199Test {
14-
private static _199 test;
14+
private static _199.Solution1 solution1;
15+
private static _199.Solution2 solution2;
1516
private static TreeNode root;
1617

1718
@BeforeClass
1819
public static void setup() {
19-
test = new _199();
20+
solution1 = new _199.Solution1();
21+
solution2 = new _199.Solution2();
2022
}
2123

2224
@Test
2325
public void test1() {
2426
root = TreeUtils.constructBinaryTree(Arrays.asList(1, null, 3));
25-
assertEquals(Arrays.asList(1, 3), test.rightSideView(root));
27+
assertEquals(Arrays.asList(1, 3), solution1.rightSideView(root));
28+
assertEquals(Arrays.asList(1, 3), solution2.rightSideView(root));
2629
}
2730

2831
}

0 commit comments

Comments
 (0)