Binary Tree Right Side View
Tree BFS
Problem
Thought Process
Solution
from collections import deque
class Solution:
def rightSideView(self, root: TreeNode) -> List[int]:
if not root:
return []
queue = deque()
queue.append(root)
res = []
while queue:
levelSize = len(queue)
for i in range(0, levelSize):
currentNode = queue.popleft()
if i == levelSize-1:
res.append(currentNode.val)
if currentNode.left:
queue.append(currentNode.left)
if currentNode.right:
queue.append(currentNode.right)
return res
#Time: O(n)
#Space: O(n)Last updated