엔지니어 게시판
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
전체 396 / 1 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 177 명
  • 오늘 방문자 5,480 명
  • 어제 방문자 5,697 명
  • 최대 방문자 11,134 명
  • 전체 회원수 1,095 명
알림 0