-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path199.py
29 lines (27 loc) · 901 Bytes
/
199.py
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
# https://neetcode.io/problems/binary-tree-right-side-view
# https://leetcode.com/problems/binary-tree-right-side-view/description/
from typing import Optional, List
from collections import deque
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
res = []
q = deque([root])
while q:
newQ = deque()
foundRight = False
while q:
n = q.popleft()
if n:
newQ.append(n.right)
newQ.append(n.left)
if not foundRight:
res.append(n.val)
foundRight = True
q = newQ
return res