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()]

最新文章

  1. Qt 程序访问 sqlite 权限错误
  2. Spring1:Spring简介、环境搭建、源码下载及导入MyEclipse
  3. HTML学习笔记——frameset和marquee
  4. andriod 动态创建控件
  5. 深入了解Qt(三)之元signal和slot
  6. cocos2d-html5对话界面设计
  7. jQuery.validate errorPlacement
  8. 九度OJ 1501 最大连续子序列乘积 -- 动态规划
  9. 关于C语言指针的问题
  10. [Docker] Docker Client in Action
  11. ECG信号读出,检测QRS,P,T 波(小波去噪,并根据检测),基于BP辨识的神经网络
  12. SQL点滴7—使用SQL Server的attach功能出现错误及解决方法
  13. Bootstrap入门(四)表格
  14. Git 常用命令速查表(图文+表格)
  15. python类的语法和底层实现
  16. Oracle条件判断if...elsif
  17. 使用Calendar获取上一月,下一月,上一年,下一年的当天日期
  18. HBase Snapshot原理和实现
  19. 2019年2月编程语言最新排行:java稳居第一(java优势在哪里)
  20. MySQL或MariaDB忘记root密码

热门文章

  1. 5.mysql的explain的分析
  2. js var
  3. vuex中的state、mutations 、actions 、getters四大属性如何使用
  4. BIP设计器代码不生效的问题解决方案
  5. mmdetection3d安装
  6. pdf2docx:可将 PDF 转换成 docx 文件的 Python 库
  7. java 守护线程的关闭
  8. rust在windows上编译成liunx可执行程序
  9. H5 判断当前浏览器环境是 微信还是支付宝
  10. Day 22 22.1.1:增量式爬虫 - 场景1的实现