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