[LeetCode 199] Binary Tree Right Side View

IEDA激活码失效?教你5分钟快速续期的方法 本文针对IEDA激活码失效问题,提供了一套从快速应急到深度排查的完整解决方案。文章不仅详解了5分钟快速续期与清理缓存等应急方法,更深入剖析了授权机制、离线激活流程,并指导如何构建可持续的授权管理策略,帮助开发者彻底解决激活困扰,确保开发环境稳定。 阅读详情

题目链接:binary-tree-right-side-view


import java.util.ArrayList;
import java.util.List;


/**
 * 
	Given 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.
	
	For example:
	Given the following binary tree,
	   1            <---
	 /   \
	2     3         <---
	 \     \
	  5     4       <---
	You should return [1, 3, 4].
 *
 */

public class BinaryTreeRightSideView {

	public class TreeNode {
		int val;
		TreeNode left;
		TreeNode right;

		TreeNode(int x) {
			val = x;
		}
	}
	
	//解法一, 层次遍历法
//	210 / 210 test cases passed.
//	Status: Accepted
//	Runtime: 300 ms
//	Submitted: 0 minutes ago
	
	//时间复杂度O(n),空间复杂度O(n)
	public List<Integer> rightSideView(TreeNode root) {
		return bfs(root);
	}
    public List<Integer> bfs(TreeNode root) {
        List<Integer> result = new ArrayList<Integer>();
        List<TreeNode> levelOrder = new ArrayList<TreeNode>();
        if(root == null)	return result;
        
        levelOrder.add(root);
        while(!levelOrder.isEmpty()) {
        	int len = levelOrder.size();
        	result.add(levelOrder.get(len - 1).val);
        	for(int i = 0; i < len; i ++) {
        		TreeNode node = levelOrder.remove(0);
        		if(node.left != null)	levelOrder.add(node.left);
        		if(node.right != null)	levelOrder.add(node.right);
        	}
        }
        return result;
    }
    
    //解法二:深度遍历法
//    210 / 210 test cases passed.
//    Status: Accepted
//    Runtime: 229 ms
//    Submitted: 0 minutes ago

    //时间复杂度O(n),空间复杂度O(n)
	public List<Integer> rightSideView1(TreeNode root) {
        List<Integer> result = new ArrayList<Integer>();
        dfs(root, result, 0);
		return result;
	}
    public void dfs(TreeNode root, List<Integer> result, int level) {
    	if(root == null) return;
    	if(level == result.size())
    		result.add(root.val);
    	dfs(root.right, result, level + 1);
    	dfs(root.left, result, level + 1);
    }
    
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}


自定义LayoutManager实现android-pile-layout滑动卡片堆叠效果 这是我的第一篇博客,总算有个契机开始写博客了,就从自定义LayoutManager开始吧。 前段时间看到一个项目(android-pile-layout),效果特别不错,这是效果图: 效果确实不错,我看了下实现,是用的自定义的ViewGroup实现的,计算什么还搞的挺复杂的,具体的没细看。作者说也尝试过用LayoutManager实现,但是没找到合适的数学模型,好吧,看到数学模型我 阅读详情

相关推荐

从选型到调试:MCU外部晶振电路设计的实战避坑指南

本文详细解析了MCU外部晶振电路设计的关键要点,从选型到调试提供全面指南。重点探讨了晶振频率选择、负载电容计算、PCB布局优化等核心问题,并分享常见故障排查技巧与EMC设计经验,帮助工程师规避设计陷阱,提升嵌入式系统时钟稳定性。

weixin_30872577的博客 284

LeetCode二叉树系列——199二叉树的右视图

刚刚我们说过了二叉树有两种存储方式顺序存储,和链式存储,顺序存储就是用数组来存,这个定义没啥可说的,我们来看看链式存储的二叉树节点的定义方式。intval;};大家会发现二叉树的定义和链表是差不多的,相对于链表,二叉树的节点里多了一个指针,有两个指针,指向左右孩子。这里要提醒大家要注意二叉树节点定义的书写方式。在现场面试的时候面试官可能要求手写代码,所以数据结构的定义以及简单逻辑的代码一定要锻炼白纸写出来。二叉树是一种基础数据结构,在算法面试中都是常客,也是众多数据结构的基石。...

w20001118的博客 1292

ESP32S3与ILI9341 LCD屏LVGL移植实战:从零构建嵌入式GUI界面

本文详细介绍了如何在ESP32S3平台上移植LVGL图形库并驱动ILI9341 LCD屏幕,从硬件选型、开发环境配置到显示驱动实现和内存优化。通过实战案例讲解移植过程中的关键步骤和常见问题解决方案,帮助开发者快速构建嵌入式GUI界面。

h9j8k7l6m5n的博客 1104

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

【墨染】层序遍历的迭代&根右左递归。

墨染 718

告别手动!用Python+CATIA V5/V6自动生成三视图和标题栏(附完整代码)

本文详细介绍了如何利用Python和CATIA V5/V6实现工程图自动生成,包括三视图布局和标题栏填写。通过COM接口调用和智能算法,系统能自适应不同尺寸模型,大幅提升设计效率。附完整代码和部署方案,帮助团队实现从手动到自动化的转变。

weixin_42699679的博客 357

CPLD新手必看:安路EF2系列配置与烧录的避坑指南(含完整流程)

本文为CPLD新手提供了安路EF2系列芯片从硬件设计到软件烧录的完整避坑指南。重点解析了电源系统、时钟复位、JTAG接口等关键电路设计要点,并详细介绍了使用TD软件进行工程创建、代码编写、引脚分配及程序烧录的全流程,帮助开发者快速上手并规避常见问题。

weixin_29217235的博客 374

LeetCode题练习与总结:二叉树的右视图--199

本文详细阐述了使用广度优先搜索解决二叉树右侧视图问题的方法,包括解题思路、代码实现、时间空间复杂度分析,以及涉及的关键知识点,为理解和实现该问题提供了全面指导。

一直学习永不止步 625

三维模型转变为点云模型的好工具——CloudCompare使用小记

普通三维模型能够直观地展示物体的三维全貌,但是在一些场合需要将三维模型变为相应的点云模型进行展示,而使用激光雷达等传感器采集到的点云信息往往只能反应模型表面的情况,或是使用PCL库中的可执行文件进行转换,这两种操作较为麻烦。笔者最近就遇到了这样一个情况,找到了一个名叫CloudCompare(CC)的软件,能够支持将多种文件格式的三维模型转变为点云模型。以下将本人在使用过程中所使用到的一些操作简记下来供朋友们和将来的自己参考。 一、下载 CloudCompare是一款免费软件,其可以在CloudComp

hdlwyt的博客 2万+

LeetCode199之二叉树的右视图(二叉树的深度遍历和广度遍历)

这题如果没有接触过二叉树的深度遍历和广度遍历其实是很难自己做出来的

数据与算法架构提升之路专栏 752

微信小程序 - {“errMsg”:”requestPayment:fail banned”} 微信小程序调用wx.requestPayment支付失败返回错误(uniapp微信小程序微信支付报错)

{“errMsg”:”requestPayment:fail banned”},微信小程序调用wx.requestPayment报错requestPayment:fail banned,uni-app微信小程序wx.requestPayment支付报错requestPayment:fail banned,拉起小程序支付报错,iPhone苹果ios真机微信支付报错,Android安卓手机微信支付报错,支付报错,商户配置,签名验证,uniapp 小程序支付微信支付一场,设置过b2b类目,确认了商户号相关功能正常

🏆 前端领域优质创作者 3373

Docker/Hyper-V/Wsl2等虚拟化组件一键切换工具 (附源码, Docker Desktop开发模式与三角洲游戏模式一键切换工具)

开启 Docker、Hyper-V 或 WSL2 后游戏无法启动?🎮💻 本文教你用一键批处理工具⚡快速切换“开发/游戏模式”,开发娱乐两不误!🔄✨

XiaoqiangClub的博客 711

leetcode 199. 二叉树的右视图 (JAVA)

题目描述: 给定一个二叉树的 根节点 root,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。 本题核心思想就是遍历二叉树,取得每一层最右边的第一个节点值。 关键点在于有几种遍历方法呢? 第一种: 广度优先搜索(BFS遍历方法) class Solution { public List<Integer> rightSideView(TreeNode root) { //res去储存最后输出的结果 List<Integer> r

迪文的博客 1038

上位机图像处理和嵌入式模块部署(qmacvisual入门)

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】 虽然我们前面学习了很多的知识点,比如说在windows这边,用qt写界面,用opencv写图像处理代码;在linux这边先用树莓派测试,最后再部署到自己的平台上面。但是,对于一个商业软件是怎么运行的,很多时候我们心理还是没有底,或者说,自己写的软件距离一个商业软件还有多远,大部分学习的同学是不知道的。 就算有的同学说,这个时候可以借鉴一下vision pro,或者是

嵌入式-老费,一个分享专业嵌入式知识的blog 1243

LeetCode 199. Binary Tree Right Side View 题解

本题是二叉树的经典问题,主要考察对二叉树遍历的理解和应用。通过使用广度优先搜索,我们可以高效地获取二叉树的右视图。广度优先搜索的核心思想是:按层遍历二叉树,对于每一层,记录最后一个节点的值,即为从右侧能看到的节点。这种方法不仅适用于二叉树的右视图问题,还可以应用于许多其他需要按层遍历二叉树的场景。掌握广度优先搜索的思想,对于解决这类问题非常重要。

cannonjinx的博客 4061

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

【LeetCode】199. Binary Tree Right Side View 二叉树的右视图(Medium)(JAVA)

【LeetCode】199. Binary Tree Right Side View 二叉树的右视图(Medium)(JAVA) 题目地址: https://leetcode.com/problems/binary-tree-right-side-view/ 题目描述: Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see order

qq_16927853的博客 186

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

LeetCode 199 Binary Tree Right Side View

LeetCode 199 Binary Tree Right Side View Problem Description: 这道题目给出一棵二叉树,输入从右侧看树得到的结点值。 具体的题目信息: https://leetcode.com/problems/binary-tree-right-side-view/description/ Example: Solution: 解题...

萤火虫啊飞呀飞 291

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

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 nodes you can see ordered from top to bottom. For example: Given the following binary tree, 1

无名山丘,崛起成峰 982

leetcode199. Binary Tree Right Side View

题目描述 Given 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. Example: Input: [1,2,3,null,5,null,4] Output: [1, 3,...

中华小当家的博客 191

LeetCode: 199. Binary Tree Right Side View

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 nodes you can see ordered from top to bottom. Exampl...

杨领well的专栏 177

199. Binary Tree Right Side View Leetcode Python

Given 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. For example: Given the following binary tree,# Defini

hyperbolechi的专栏 1284

【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 nodes you can see ordered from top to bottom. 思路: 先序遍历(先右后左)。max表示已经访问的最大深度,只有比这个max更大时才会保存到ret数组(答案都放在这里)中。 代码实现: submit一次就过了: /** * Definition for a b

zxc120389574的博客 273

leetcode_199:Binary Tree Right Side View

这个题目其实没啥说的,就是层次遍历,每次都取那一层的最后一个就行。只是编码过程中有一些边界条件需要注意,再有就是这种需要保存上一层状态的题目,一般是需要两个队列的,来轮流保存每层的节点,代码如下: public List<Integer> rightSideView(TreeNode root) { List<Integer> result = ne...

reachwang的博客 419

LeetCode题解之199. Binary Tree Right Side View

文章目录问题链接问题描述解题思路思路一:递归思路二:层次遍历相关题目 问题链接 英文网站:199. Binary Tree Right Side View 中文网站:199. 二叉树的右视图 问题描述 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

supermouse的博客 792

LeetCode199 Binary Tree Right Side View

LeetCode199 Binary Tree Right Side View 问题来源LeetCode199 问题描述 Given 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 b

yanqinghe123的专栏 291

leetcode-199 Binary Tree Right Side View

问题描述地址: https://leetcode.com/problems/binary-tree-right-side-view/描述: Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from

hit_ynsun的博客 288

Leetcode 199: Binary Tree Right Side View

Problem:Given 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.For example: Given the following binary tree,

哈哈的个人专栏 1722
上一篇: [LeetCode 200] Number of Islands
下一篇: LeetCode 在矩阵中寻找路径类的动态规划问题
xiaoo_gan
博客等级 码龄16年 20粉丝 165原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值