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

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

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

    法一: 自己的代码

    构建一个队列,每次都从右边取值,用双层循环实现层序遍历.

    # 执行用时 :32 ms, 在所有 python3 提交中击败了98.01% 的用户
    # 内存消耗 :12.8 MB, 在所有 python3 提交中击败了100.00%的用户
    # Definition for a binary tree 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, root: TreeNode) -> List[int]:
            result = []
            if 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
  • 相关阅读:
    归并排序
    mysql优化
    树结构
    urllib库的使用
    linux常用命令
    mysql慢查询
    支付宝第三方支付
    类型(type)判断
    c语言自带的排序与查找
    多字节与宽字节转换
  • 原文地址:https://www.cnblogs.com/xxswkl/p/12012557.html
Copyright © 2011-2022 走看看