首页 > 其他分享 >Leetcode 199

Leetcode 199

时间:2022-12-27 23:33:57浏览次数:63  
标签:right 199 val self rightmost Leetcode left

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

标签:right,199,val,self,rightmost,Leetcode,left
From: https://www.cnblogs.com/selfmade-Henderson/p/17009241.html

相关文章

  • 中国各省绿色专利维持年限数据(1990-2022)
    中国各省绿色专利维持年限数据(1990-2022)中国各省绿色专利维持年限数据(1990-2022)中国各省绿色专利维持年限数据(1990-2022) 最新版数据已整理为Excel格式,数据的时间区......
  • 中国各省高质量发展指数数据(1990-2021)
    中国各省高质量发展指数数据(1990-2021)中国各省高质量发展指数数据(1990-2021)中国各省高质量发展指数数据(1990-2021) 最新版数据已整理为Excel格式,数据的时间区间为199......
  • 中国各省绿色专利申请数据(1990-2022)
    中国各省绿色专利申请数据(1990-2022)中国各省绿色专利申请数据(1990-2022)中国各省绿色专利申请数据(1990-2022) 最新版数据已整理为Excel格式,数据的时间区间为1990-2022......
  • 中国各省绿色专利授权数据(1990-2022)
    中国各省绿色专利授权数据(1990-2022)中国各省绿色专利授权数据(1990-2022)中国各省绿色专利授权数据(1990-2022) 最新版数据已整理为Excel格式,数据的时间区间为1990-2022年......
  • [leetcode]第 7 天 搜索与回溯算法(简单)
    26.树的子结构思路不知道。。看大佬的题解流程:先判断B是不是以A节点为根节点的一个子树如果不是,判断B是否是A左右子树的一个子结构isSubTree(Ta,Tb)判断Tb是否是......
  • leetcode-27 移除元素
    27.移除元素给你一个数组nums和一个值val,你需要原地移除所有数值等于val的元素,并返回移除后数组的新长度。不要使用额外的数组空间,你必须仅使用O(1)额外空间并原......
  • #yyds干货盘点# LeetCode程序员面试金典:求和路径
    题目:给定一棵二叉树,其中每个节点都含有一个整数数值(该值或正或负)。设计一个算法,打印节点数值总和等于某个给定值的所有路径的数量。注意,路径不一定非得从二叉树的根节点或......
  • #yyds干货盘点# LeetCode程序员面试金典:插入
    题目:给定两个整型数字 N​ 与 M​,以及表示比特位置的 i​ 与 j(i<=j,且从0位开始计算)。编写一种方法,使 M​ 对应的二进制数字插入 N​ 对应的二进制数字的第 i......
  • leetcode笔记——325周赛
    2515.到目标字符串的最短距离-力扣(LeetCode)这道题一次遍历就可以做,直接用abs(i-startindex)和n-abs(i-startindex)即可表示距离,但我做的时候绕麻烦了......
  • leetcode-541. 反转字符串 II
    541.反转字符串II-力扣(Leetcode)比较简单,想清楚边界条件,然后做一下字符的反转即可。go可以将不能变动的字符串转换成可以变动的[]byte之后,修改完之后,再转成string......