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. 多线程问题sleep与wait

    涉及到的三个方法:wait():一旦执行此方法,当前线程就进入阻塞状态,并释放同步监视器notify():一旦执行此方法,就会唤醒被wait的一个线程.如果有多个线程被wait,就唤醒优先级高:not ...

  2. Symfony2在Nginx下的配置方法图文教程

    来源: https://www.xp.cn/b.php/79706.html Symfony2在Nginx下的配置方法图文教程 本文详细讲述了Symfony2在Nginx下的配置方法.分享给大家供大家 ...

  3. geoserver的自动化部署

    年后接到一个任务,需求是这样的: 搭建一个geoserver服务器,将公司内部的mbtile数据(EPSG:3857)发布出去 服务的输出格式为MBTiles with vector tiles的矢量 ...

  4. C与C++字符串比较

    #include<iostream> #include<string> using namespace std; int main() { char a[] = "a ...

  5. flink udaf函数

    1.Flink-sql自定义UDAF函数 - 简书 (jianshu.com) 2.Flink SQL 自定义UDAF_k_wzzc的博客-CSDN博客_flink udaf 3.Flink 实践教程 ...

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

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

  7. go语言web框架-如何使用gin教程+react实现web项目

    go-web+ react实践项目 前端使用react 搭建,从webpack搭建开始写,后端是学习go语言过程中的实践,由于之前没有实际的后端web经验,所以是自己一点一点摸索出来的.有错漏还望指正 ...

  8. spring boot整合druid

    其实网上有很多例子可供参考,主要是在整合的过程中遇到了一些问题,方便记录下.另外例子可参考以下两个链接: https://www.jianshu.com/p/e3cd2e1c2b0c https:// ...

  9. VUE+elementUI 分页请求回显问题解决方案

    一直专注写后台的本人,之前新产品回显问题,一直没处理,这对实施配置系统,会产生很大影响 由于写页面的同事要离职,一直在游泳,只能自己上手去干了.本人对 vue 和elementUI 处于一知半解,所以 ...

  10. C语言-链表流星雨(EsayX)

    刷B站看到的,做个玩玩.IDE:Visual Studio 2022.依赖EsayX图形库 1-效果 2-程序 /* 链表流星雨单文件版本 依赖EsayX图形库 */ #include <std ...