Skip to content

Commit 9ab8b79

Browse files
authored
feat: 199. Binary Tree Right Side View (#44)
Binary tree bfs Medium
1 parent 8725f8d commit 9ab8b79

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

src/binary_tree/bfs/__init__.py

Whitespace-only changes.

src/binary_tree/bfs/binary_tree_right_side_view/__init__.py

Whitespace-only changes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import Optional, List
2+
from collections import deque
3+
from structures import TreeNode
4+
5+
6+
class Solution:
7+
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
8+
result = []
9+
q = deque()
10+
q.append(root)
11+
12+
while q:
13+
right_side = None
14+
for _ in range(len(q)):
15+
node = q.popleft()
16+
if node:
17+
right_side = node
18+
q.append(node.left)
19+
q.append(node.right)
20+
if right_side:
21+
result.append(right_side.val)
22+
return result
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
from src.binary_tree.bfs.binary_tree_right_side_view.solution import Solution
3+
from test_utils import TreeBuilder
4+
5+
6+
@pytest.mark.parametrize(
7+
"root, expected",
8+
[
9+
([1, 2, 3, None, 5, None, 4], [1, 3, 4]),
10+
([1, 2, 3, 4, None, None, None, 5], [1, 3, 4, 5]),
11+
([1, None, 3], [1, 3]),
12+
([], []),
13+
],
14+
)
15+
def test_max_depth(root, expected):
16+
root = TreeBuilder.from_list(root)
17+
solution = Solution()
18+
assert solution.rightSideView(root) == expected

0 commit comments

Comments
 (0)