Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit284e03f

Browse files
refactor 199
1 parent4194813 commit284e03f

File tree

1 file changed

+49
-60
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+49
-60
lines changed

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

Lines changed: 49 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,69 +7,58 @@
77
importjava.util.List;
88
importjava.util.Queue;
99

10-
/**199. Binary Tree Right Side View
11-
12-
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
13-
14-
For example:
15-
Given the following binary tree,
16-
17-
1 <---
18-
/ \
19-
2 3 <---
20-
\ \
21-
5 4 <---
22-
23-
You should return [1, 3, 4]. */
24-
2510
publicclass_199 {
2611

27-
publicstaticclassSolution1 {
28-
/**credit: https://leetcode.com/problems/binary-tree-right-side-view/discuss/56012/My-simple-accepted-solution(JAVA)*/
29-
publicList<Integer>rightSideView(TreeNoderoot) {
30-
List<Integer>result =newArrayList<>();
31-
rightView(root,result,0);
32-
returnresult;
33-
}
12+
publicstaticclassSolution1 {
13+
/**
14+
* credit: https://leetcode.com/problems/binary-tree-right-side-view/discuss/56012/My-simple-accepted-solution(JAVA)
15+
*/
16+
publicList<Integer>rightSideView(TreeNoderoot) {
17+
List<Integer>result =newArrayList<>();
18+
rightView(root,result,0);
19+
returnresult;
20+
}
3421

35-
voidrightView(TreeNodecurr,List<Integer>result,intcurrDepth) {
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-
}
22+
voidrightView(TreeNodecurr,List<Integer>result,intcurrDepth) {
23+
if (curr ==null) {
24+
return;
25+
}
26+
if (currDepth ==result.size()) {
27+
result.add(curr.val);
28+
}
29+
rightView(curr.right,result,currDepth +1);
30+
rightView(curr.left,result,currDepth +1);
31+
}
32+
}
4633

47-
publicstaticclassSolution2 {
48-
/**BFS the tree*/
49-
publicList<Integer>rightSideView(TreeNoderoot) {
50-
List<Integer>result =newArrayList<>();
51-
if (root ==null) {
52-
returnresult;
53-
}
54-
Queue<TreeNode>q =newLinkedList<>();
55-
q.offer(root);
56-
while (!q.isEmpty()) {
57-
intsize =q.size();
58-
for (inti =0;i <size;i++) {
59-
TreeNodecurr =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-
}
69-
}
70-
}
71-
returnresult;
72-
}
73-
}
34+
publicstaticclassSolution2 {
35+
/**
36+
* BFS the tree
37+
*/
38+
publicList<Integer>rightSideView(TreeNoderoot) {
39+
List<Integer>result =newArrayList<>();
40+
if (root ==null) {
41+
returnresult;
42+
}
43+
Queue<TreeNode>q =newLinkedList<>();
44+
q.offer(root);
45+
while (!q.isEmpty()) {
46+
intsize =q.size();
47+
for (inti =0;i <size;i++) {
48+
TreeNodecurr =q.poll();
49+
if (i ==size -1) {
50+
result.add(curr.val);
51+
}
52+
if (curr.left !=null) {
53+
q.offer(curr.left);
54+
}
55+
if (curr.right !=null) {
56+
q.offer(curr.right);
57+
}
58+
}
59+
}
60+
returnresult;
61+
}
62+
}
7463

7564
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp