Skip to content

Commit 6073ef9

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 199 Binary Tree Right Side View
1 parent 622ab73 commit 6073ef9

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
5858
- [189 Rotate Array](https://leetcode.com/problems/rotate-array/description/)
5959
- [190 Reverse Bits](https://leetcode.com/problems/reverse-bits/description/)
6060
- [191 Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/description/)
61+
- [199 Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view/description/)
6162
- [201 Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/description/)
6263
- [202 Happy Number](https://leetcode.com/problems/happy-number/description/)
6364
- [205 Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/description/)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import List, Optional
2+
3+
from awesome_python_leetcode.tree import TreeNode
4+
5+
6+
class Solution:
7+
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
8+
"""
9+
Given the root of a binary tree, imagine yourself standing on the right side of
10+
it, return the values of the nodes you can see ordered from top to bottom.
11+
"""
12+
if root is None:
13+
return []
14+
15+
parents = [root]
16+
result = []
17+
while parents:
18+
# Get right side of cur level
19+
result.append(parents[0].val)
20+
21+
# Build childs
22+
childs = []
23+
for p in parents:
24+
if p.right:
25+
childs.append(p.right)
26+
if p.left:
27+
childs.append(p.left)
28+
parents = childs
29+
return result
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._199_binary_tree_right_side_view import Solution
6+
from awesome_python_leetcode.tree import TreeNode
7+
8+
9+
@pytest.mark.parametrize(
10+
argnames=["root", "expected"],
11+
argvalues=[
12+
([1, 2, 3, None, 5, None, 4], [1, 3, 4]),
13+
([1, 2, 3, 4, None, None, None, 5], [1, 3, 4, 5]),
14+
([1, None, 3], [1, 3]),
15+
([], []),
16+
],
17+
)
18+
def test_func(root: List[int], expected: List[int]):
19+
root = TreeNode.build(root)
20+
right_side_view = Solution().rightSideView(root)
21+
assert right_side_view == expected

0 commit comments

Comments
 (0)