Skip to content

Commit a08f4f4

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.9 MB (6.39%)
1 parent d4b1934 commit a08f4f4

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<p>Given the <code>root</code> of a binary tree, imagine yourself standing on the <strong>right side</strong> of it, return <em>the values of the nodes you can see ordered from top to bottom</em>.</p>
2+
3+
<p>&nbsp;</p>
4+
<p><strong class="example">Example 1:</strong></p>
5+
6+
<div class="example-block">
7+
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,null,5,null,4]</span></p>
8+
9+
<p><strong>Output:</strong> <span class="example-io">[1,3,4]</span></p>
10+
11+
<p><strong>Explanation:</strong></p>
12+
13+
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/11/24/tmpd5jn43fs-1.png" style="width: 400px; height: 207px;" /></p>
14+
</div>
15+
16+
<p><strong class="example">Example 2:</strong></p>
17+
18+
<div class="example-block">
19+
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,null,null,null,5]</span></p>
20+
21+
<p><strong>Output:</strong> <span class="example-io">[1,3,4,5]</span></p>
22+
23+
<p><strong>Explanation:</strong></p>
24+
25+
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/11/24/tmpkpe40xeh-1.png" style="width: 400px; height: 214px;" /></p>
26+
</div>
27+
28+
<p><strong class="example">Example 3:</strong></p>
29+
30+
<div class="example-block">
31+
<p><strong>Input:</strong> <span class="example-io">root = [1,null,3]</span></p>
32+
33+
<p><strong>Output:</strong> <span class="example-io">[1,3]</span></p>
34+
</div>
35+
36+
<p><strong class="example">Example 4:</strong></p>
37+
38+
<div class="example-block">
39+
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
40+
41+
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
42+
</div>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
49+
<li><code>-100 &lt;= Node.val &lt;= 100</code></li>
50+
</ul>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Approach 3: BFS: One Queue + Level Size Measurements
2+
3+
# Time: O(n)
4+
# Space: O(d), d = tree diameter
5+
6+
from collections import deque
7+
8+
# Definition for a binary tree node.
9+
# class TreeNode:
10+
# def __init__(self, val=0, left=None, right=None):
11+
# self.val = val
12+
# self.left = left
13+
# self.right = right
14+
15+
class Solution:
16+
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
17+
if root is None:
18+
return []
19+
20+
queue = deque([root])
21+
rightside = []
22+
23+
while queue:
24+
level_length = len(queue)
25+
26+
for i in range(level_length):
27+
node = queue.popleft()
28+
29+
if i == level_length - 1:
30+
rightside.append(node.val)
31+
32+
if node.left:
33+
queue.append(node.left)
34+
if node.right:
35+
queue.append(node.right)
36+
37+
return rightside
38+

0 commit comments

Comments
 (0)