LeetCode [中等]二叉树的右视图(层序

news/2024/7/20 22:12:41 标签: leetcode, 深度优先, 算法

199. 二叉树的右视图 - 力扣(LeetCode)

从二叉树的层序遍历改进,根右左

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
public class Solution {
    public List<int> res = new List<int>();
    public IList<int> RightSideView(TreeNode root) {
        if(root == null)
            return res.ToArray();
 
        DFS(root,0);
        return res.ToArray();
    }

    private void DFS(TreeNode root, int level)
    {
        if(root == null)
            return;
        
        if(res.Count < level + 1)
        {
            res.Add(root.val);
        }

     DFS(root.right, level + 1);
     DFS(root.left, level + 1);
    }


}


http://www.niftyadmin.cn/n/5234755.html

相关文章

XML处理相关——学习一些处理脚本(1)——(待完善)

python脚本 import osfrom Bio.Blast import NCBIXMLimport FetchUtil #https://github.com/NiallScott/FetchUtils import randomif not os.path.exists(XML):os.mkdir(XML) if not os.path.exists(dicts):os.mkdir(dicts)def bestrecipblast(org, seed, thresh5, queueNone)…

(C语言)找出1-99之间的全部同构数

同构数&#xff1a;它出现在平方数的右边。例&#xff1a;5是25右边的数&#xff0c;25是625右边的数&#xff0c;即5和25均是同构数。 #include<stdio.h> int main() {for(int i 1;i < 100;i ){if((i*i % 10 i) || (i*i % 100 i))printf("%d\t%d\n",i,…

docker安装达梦数据库并挂在数据卷

1.在线下载 wget https://download.dameng.com/eco/dm8/dm8_20230808_rev197096_x86_rh6_64_single.tar2. 导入镜像 docker load -i dm8_20230808_rev197096_x86_rh6_64_single.tar3. 运行容器 docker run -d -p 5236:5236 --restartalways --name dm8_test --privilegedtru…

Java基础- 面向对象三大特性

一、封装&#xff08;Encapsulation&#xff09; 概念&#xff1a;将对象的属性和服务结合为一个独立的整体&#xff0c;并尽可能隐藏内部的实现。 理解&#xff1a;封装的意义有两个&#xff0c;一是用访问控制符决定客户端程序员可以使用类的哪些部分&#xff0c;只把必要的…

基于社区电商的Redis缓存架构-库存模块缓存架构(下)

基于缓存分片的下单库存扣减方案 将商品进行数据分片&#xff0c;并将分片分散存储在各个 Redis 节点中&#xff0c;那么如何计算每次操作商品的库存是去操作哪一个 Redis 节点呢&#xff1f; 我们对商品库存进行了分片存储&#xff0c;那么当扣减库存的时候&#xff0c;操作…

Linux下Redis安装及配置

首先下载redis安装包&#xff1a;地址 这里我使用的是7.0版本的&#xff01; 将文件上传至linux上&#xff0c;此处不再多叙述&#xff0c;不会操作的&#xff0c;建议使用ftp&#xff01; 第一步&#xff1a;解压压缩包 tar -zxvf redis-7.0.14.tar.gz第二步&#xff1a;移…

react-native实践日记--6.ReactNative 项目版本升级,0.61到0.72升级的问题记录(二)

接上一篇&#xff0c;ReactNative项目升级&#xff0c;0.61到0.72的升级问题。 在升级过程中&#xff0c;发现react-native-scrollable-tab-view这个tab插件&#xff0c;这是一个tab导航栏插件&#xff0c;可以实现切换页面、页面滚动、下拉刷新、上拉加载更多等&#xff0c;功…

Python海绵宝宝

目录 系列文章 写在前面 海绵宝宝 写在后面 系列文章 序号文章目录直达链接表白系列1浪漫520表白代码https://want595.blog.csdn.net/article/details/1306668812满屏表白代码https://want595.blog.csdn.net/article/details/1297945183跳动的爱心https://want595.blog.cs…