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. BAPI_GOODSMVT_CREATE - 101 mvt. Message ERROR M7427

    Message SAP M7427 - Entered batch &1 does not match batch &2 in the purchase order 639934 - ...

  2. webrtc 问题记录

    参考:https://webrtc.org.cn/mirror/ 设置代理那里必须设置代理再同步 1.上一篇博文, git clone 失败 报错:fatal: unable to access,se ...

  3. js扩展符号

    扩展数组:const arr23 = ['a', 'b', 'c']; console.log(...arr23); a b c扩展字符串: abc = 'abcdefghi'; console.lo ...

  4. h5py学习(一)核心概念

    因pandas的to_hdf5函数有bug TypeError: object of type 'int' has no len(),写dataframe数据出现了报错,遂决定直接使用h5py来写数据 ...

  5. 如何快速下载xcode等官方app

    为了避免xcode ghost类的风险,用app store又是如此的卡,如何解决呢? https://developer.apple.com/downloads/ 这里各种版本的下载. 绝对官方签名 ...

  6. JVMCFRE003 bad major version问题

    项目启动异常日志 [21-11-18 23:46:58:166 CST] 00000020 DispatcherSer I org.springframework.web.servlet.Framew ...

  7. cc1

    基础 cc接口及类介绍 Transformer接口 Defines a functor interface implemented by classes that transform one obje ...

  8. php基础教程(一)

    语法: PHP的语法很简单 --直接看代码: <?php /*代码部分*/ ?> 这就是PHP代码的声明方式. 注:<? ?> 等这中写法也可以写,但是不建议这么写. 标记语句 ...

  9. vue的表单

    你可以用 v-model 指令在表单控件元素上创建双向数据绑定. 输入框 实例中演示了 input 和 textarea 元素中使用 v-model 实现双向数据绑定: <!DOCTYPE ht ...

  10. spark项目技术点整理

    spark项目技术点整理 1.性能调优: 1>分配更多的资源:性能调优的王道就是分配和增加更多的资源.写完一个spark作业后第一个要是调节最优的资源配置,能够分配的资源达到你的能力范围的顶端后 ...