Skip to content

Commit 97c3385

Browse files
solves right side view in java
1 parent 121971b commit 97c3385

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@
158158
| 190 | [Reverse Bits](https://leetcode.com/problems/reverse-bits) | [![Java](assets/java.png)](src/ReverseBits.java) [![Python](assets/python.png)](python/reverse_bits.py) | |
159159
| 191 | [Number of One Bits](https://leetcode.com/problems/number-of-1-bits) | [![Java](assets/java.png)](src/NumberOf1Bit.java) [![Python](assets/python.png)](python/number_of_1_bits.py) | |
160160
| 198 | [House Robber](https://leetcode.com/problems/house-robber) | [![Java](assets/java.png)](src/HouseRobber.java) [![Python](assets/python.png)](python/house_robber.py) | |
161-
| 199 | [Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view) | | |
161+
| 199 | [Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view) | [![Java](assets/java.png)](src/BinaryTreeRightSideView.java) | |
162162
| 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands) | | |
163163
| 201 | [Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range) | | |
164164
| 202 | [Happy Number](https://leetcode.com/problems/happy-number) | [![Java](assets/java.png)](src/HappyNumber.java) [![Python](assets/python.png)](python/happy_number.py) | |

src/BinaryTreeRightSideView.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// https://leetcode.com/problems/binary-tree-right-side-view
2+
// T: O(n)
3+
// S: O(log(n))
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class BinaryTreeRightSideView {
9+
public List<Integer> rightSideView(TreeNode root) {
10+
final List<Integer> result = new ArrayList<>();
11+
rightSideView(root, result, 0);
12+
return result;
13+
}
14+
15+
private void rightSideView(TreeNode root, final List<Integer> result, int level) {
16+
if (root == null) return;
17+
if (level == result.size()) result.add(root.val);
18+
19+
rightSideView(root.right, result, level + 1);
20+
rightSideView(root.left, result, level + 1);
21+
}
22+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy