-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTreeRightSideView.java
66 lines (54 loc) · 1.45 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package binarytree;
import java.util.*;
import binarytree.BinaryTreeTest.TreeNode;
/**
* @author Shogo Akiyama
* Solved on 07/03/2020
*
* 199. Binary Tree Right Side View
* https://leetcode.com/problems/binary-tree-right-side-view/
* Difficulty: Medium
*
* Approach: BFS and Queue
* Runtime: 2 ms, faster than 17.52% of Java online submissions for Binary Tree Right Side View.
* Memory Usage: 40.3 MB, less than 5.04% of Java online submissions for Binary Tree Right Side View.
*
* Time Complexity: O(n)
* Space Complexity: O(n)
* Where n is the number of nodes in the tree
*
* @see BinaryTreeTest#testBinaryTreeRightSideView()
*/
public class BinaryTreeRightSideView {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
if (root == null) {
return res;
}
Queue<TreeNode> que = new ArrayDeque<TreeNode>();
Queue<Integer> levels = new ArrayDeque<Integer>();
TreeNode prev = null;
int level = 0;
que.offer(root);
levels.offer(0);
while (!que.isEmpty()) {
TreeNode currNode = que.poll();
Integer currLevel = levels.poll().intValue();
if (currLevel > level) {
res.add(prev.val);
level = currLevel;
}
if (currNode.left != null) {
que.offer(currNode.left);
levels.offer(currLevel + 1);
}
if (currNode.right != null) {
que.offer(currNode.right);
levels.offer(currLevel + 1);
}
prev = currNode;
}
res.add(prev.val);
return res;
}
}