We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent af9b75f commit ccbcfb0Copy full SHA for ccbcfb0
java/199-Binary-Tree-Right-Side-View.java
@@ -0,0 +1,23 @@
1
+class Solution {
2
+ public List<Integer> rightSideView(TreeNode root) {
3
+ List<Integer> list = new ArrayList<Integer>();
4
+ if (root == null) return list;
5
+ bfs(list, root);
6
+ return list;
7
+ }
8
+
9
+ public void bfs(List<Integer> list, TreeNode root) {
10
+ Queue<TreeNode> q = new LinkedList<>();
11
+ q.offer(root);
12
+ while (!q.isEmpty()) {
13
+ int levelSize = q.size();
14
+ for (int i = 0; i<levelSize; i++) {
15
+ TreeNode cur = q.poll();
16
+ if (i==0) list.add(cur.val);
17
+ if (cur.right!=null) q.offer(cur.right);
18
+ if (cur.left!=null) q.offer(cur.left);
19
20
21
22
23
+}
0 commit comments