199. Binary Tree Right Side View

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.

Analysis: given the depth, each depth we can only see the rightmost node(leaf) in exists. So actually the question is asking for a DFS for the rightmost nodes each stage.

So we can have a BFS, keep each level's rightmost node in a queue. Then output the nodes in the queue.

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
 
class Solution:
    def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
    d = {}
    def f(r,i):
      if r:
        d[i] = r.val
        f(r->right, i+1)
        f(r->left, i+1)
    
    f(root,0)
    return  d[*values()]

Leetcode 199的更多相关文章

  1. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  2. leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II

    leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  3. LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)

    199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Giv ...

  4. leetcode@ [199] Binary Tree Right Side View (DFS/BFS)

    https://leetcode.com/problems/binary-tree-right-side-view/ Given a binary tree, imagine yourself sta ...

  5. [LeetCode] 199. Binary Tree Right Side View 二叉树的右侧视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  6. leetcode.199二叉树的右视图

    给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4]输出: [1, 3, 4]解释: 1 <-- ...

  7. Java实现 LeetCode 199 二叉树的右视图

    199. 二叉树的右视图 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, ...

  8. 力扣Leetcode 199. 二叉树的右视图

    199. 二叉树的右视图 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, ...

  9. Java for LeetCode 199 Binary Tree Right Side View

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  10. (二叉树 bfs) leetcode 199. Binary Tree Right Side View

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

随机推荐

  1. Self-Attention学习

    2个连接+1个视频推荐 Self-Attention 原理与代码实现_DonngZH的博客-CSDN博客_selfattention代码 Transformer模型详解(图解最完整版) - 知乎 (z ...

  2. 解决linux系统中对两个不同的网络连接http访问

    项目5G 问题背景:调用网络端的服务器服务,对本地机器人进行控制,以实现机器人应执行的任务. 需求:主机需要访问外网的API,同时在本地局域网与机器人的API进行通信. 问题:要解决HTTP请求发送到 ...

  3. Android MD5加密、RSA加密

    现在公司做金融项目,需要考虑安全加密方面的问题.感谢大牛同事(冯哥) ,给我很大的帮助. 考虑到安全优化,我们把秘钥.加密步骤放到native中.考虑用到的技术:(1 )jni,(2 )OpenSSL ...

  4. webapp 增加 springmvc框架 支持

    1.通过maven创建一个webapp项目,并在IDEA 中增加smart tomcat的插件. 2.然后在pom文件中添加springmvc的依赖 <!-- ServletAPI --> ...

  5. jmeter-时间处理

    ${__time(,)} 1486091280955 //无格式化参数,返回当前毫秒时间,默认13位.一般用来做时间戳 ${__time(/1000,)} //为取10位的时间戳的函数表达式(时间精确 ...

  6. 在LUbuntu上搭建Neovim+Markdown环境

    前言 想搭建自己的电子笔记系统.一开始用VMware+Ubuntu,后来想,如果这个虚拟机文件比较小,就可以用克隆到U盘里,随身带了. 于是转Lubuntu. 总体步骤 安装系统 安装neovim 安 ...

  7. 原创分享 HubbleDotNet 最新绿色版,服务端免安装,基于eaglet 最后V1.2.8.9版本开发,bug修正,支持一键生成同步表

    HubbleDotNet 是一个基于.net framework 的开源免费的全文搜索数据库组件.开源协议是 Apache 2.0.HubbleDotNet提供了基于SQL的全文检索接口,使用者只需会 ...

  8. django验证码模块django-simple-captcha的使用介绍

    django-simple-captcha是django验证码模块,非常方便易用. 1.环境的准备: 在django项目环境中安装:pip install django-simple-captcha ...

  9. Maxim遍历测试工具(monkey升级版)

    Maxim 对应GitHub地址:https://github.com/zhangzhao4444/Maxim,其是对Android monkey的改进工具.是基于遍历规则和高性能要求. 条件准备: ...

  10. 061_Apex 异常捕捉

    Trigger 中的错误处理 在 Trigger 中,我们可以为进行操作的数据进行验证,类似于验证规则.如果遇到不符合条件的数据,可以通过 addError() 函数来将错误显示给用户,并记录日志. ...