LeetCode 솔루션 분류

[7/11] 199. Binary Tree Right Side View

컨텐츠 정보

본문

Medium
7018380Add to ListShare

Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

 

Example 1:

Input: root = [1,2,3,null,5,null,4]
Output: [1,3,4]

Example 2:

Input: root = [1,null,3]
Output: [1,3]

Example 3:

Input: root = []
Output: []

 

Constraints:

  • The number of nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100

관련자료

댓글 1

학부유학생님의 댓글

  • 익명
  • 작성일
Runtime: 57 ms, faster than 33.18% of Python3 online submissions for Binary Tree Right Side View.
Memory Usage: 13.9 MB, less than 69.82% of Python3 online submissions for Binary Tree Right Side View.

import collections
class Solution:
    def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
        
        deque = collections.deque([])
        if root: deque.append(root)
            
        res = []
        
        while deque:
            res.append(deque[-1].val)
            
            for i in range(len(deque)):
                curr = deque.popleft()
                if curr.left: deque.append(curr.left)
                if curr.right: deque.append(curr.right)
        
        return res
LeetCode 솔루션 357 / 10 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


알림 0