199 binary tree right view

Title: Given a binary tree, to imagine standing on its own right, in order from the top in the end portion of the return value of the node can be seen from the right side.

Source: https://leetcode-cn.com/problems/binary-tree-right-side-view/

Act one: their own code

Construction of a queue, each value from the right, to achieve a double loop traversal sequence.

# Execute when used: 32 ms, beat the 98.01% of users in all python3 submission 
# memory consumption: 12.8 MB, defeated 100.00% of users in all python3 submission 
# Definition for A binary Tree the Node. 
Class TreeNode:
     DEF  __init__ ( Self, X): 
        self.val = X 
        self.left = None 
        self.right = None
 from Typing Import List
 class Solution:
     DEF rightSideView (Self, the root: the TreeNode) -> List [int]: 
        Result = []
         IF the root IS None:
            return []
        queue = []
        queue_next = []
        queue_next.append(root)
        while queue_next:
            queue = queue_next
            queue_next = []
            result.append(queue[0].val)
            while queue:
                a = queue.pop(0)
                if a.right:
                    queue_next.append(a.right)
                if a.left:
                    queue_next.append(a.left)
        return result
View Code

 

Guess you like

Origin www.cnblogs.com/xxswkl/p/12012557.html