zoukankan      html  css  js  c++  java
  • LeetCode 199. 二叉树的右视图

    199. 二叉树的右视图

    Difficulty: 中等

    给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。

    示例:

    输入: [1,2,3,null,5,null,4]
    输出: [1, 3, 4]
    解释:
    
       1            <---
     /   
    2     3         <---
          
      5     4       <---
    

    Solution

    Language: 全部题目

    BFS+队列实现层序遍历,一次AC。

    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    ​
    class Solution:
        def rightSideView(self, root: TreeNode) -> List[int]:
            if not root: return []
            queue, res = [root], []
    ​
            while queue:
                size = len(queue)
                for i in range(size):
                    node = queue.pop(0)
                    if i == size - 1:
                        res.append(node.val)
                    if node.left:
                        queue.append(node.left)
                    if node.right:
                        queue.append(node.right)
            return res
    
  • 相关阅读:
    ubuntu18.04 常用命令
    docker常用命令
    git
    y7000 intel nvidia 双显卡安装Ubuntu16.04
    linux中fork() 函数详解
    理解GBN协议
    C++ sort
    最近点对-分治
    方便查看 linux/kernel/system_call.s
    方便查看 linux/kernel/asm.s
  • 原文地址:https://www.cnblogs.com/swordspoet/p/14070368.html
Copyright © 2011-2022 走看看