-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreeRightSideView.java
36 lines (32 loc) · 1.15 KB
/
BinaryTreeRightSideView.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
package com.namanh.bfs;
import com.namanh.common.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* https://leetcode.com/problems/binary-tree-right-side-view
* Q: Given the root of 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.
*
* S1: Use BFS and Queue, visit from right to left child node
* S2: Each step, add first element value to result
*/
public class BinaryTreeRightSideView {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
result.add(queue.peek().val);
int curSize = queue.size();
for (int i = 0; i < curSize; i++) {
TreeNode node = queue.poll();
if (node.right != null) queue.offer(node.right);
if (node.left != null) queue.offer(node.left);
}
}
return result;
}
}