LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
199. 二叉树的右视图
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.
LeetCode199. Binary Tree Right Side View
示例:
输出: [1, 3, 4]
解释:
```
1
The core idea of this algorithm:
2. View depth is current size of result list.
Java 实现
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
import java.util.ArrayList;
import java.util.List;
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList<>();
rightView(root, result, 0);
return result;
}
public void rightView(TreeNode curr, List<Integer> result, int currDepth) {
if (curr == null) {
return;
}
if (currDepth == result.size()) {
result.add(curr.val);
}
rightView(curr.right, result, currDepth + 1);
rightView(curr.left, result, currDepth + 1);
}
}
相似题目
参考资料
- https://leetcode.com/problems/binary-tree-right-side-view/
- https://leetcode-cn.com/problems/binary-tree-right-side-view/
LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)的更多相关文章
- [Swift]LeetCode199. 二叉树的右视图 | Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- Java实现 LeetCode 199 二叉树的右视图
199. 二叉树的右视图 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, ...
- 力扣Leetcode 199. 二叉树的右视图
199. 二叉树的右视图 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, ...
- LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释: 1 ...
- leetcode.199二叉树的右视图
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4]输出: [1, 3, 4]解释: 1 <-- ...
- LeetCode 199 二叉树的右视图
题目: 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释: 1 ...
- LeetCode 199. 二叉树的右视图 C++ 用时超100%
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- LeetCode——199. 二叉树的右视图
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释: 1 < ...
- Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)
Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...
随机推荐
- Oracle自动备份(多个库),压缩打包,删除原文件
1.创建一个database_Backup.bat文件,用记事本打开 内容参考如下:(楼主这里自动备份了四个数据库) set "now=%date:~0,4%%date:~5,2%%date ...
- [转] 修改sqlserver的数据库名、物理名称和逻辑文件名
转载: https://blog.csdn.net/dym0080/article/details/81017777
- Kali Linux硬盘扩容
传送门--->http://www.kali.org.cn/thread-27079-1-1.html.kali虚拟机扩容
- C语言--二维数组
一.PTA实验作业 题目1:7-2 求整数序列中出现次数最多的数 1. 本题PTA提交列表 2. 设计思路 定义变量n表示输入整数个数,count表示每个数出现次数,i.j表示循环变量,k表示次数最多 ...
- Cent0S 6.7直接在/etc/resolv.conf文件下修改DNS地址重启不生效问题【转】
CentOS 6.7/Linux下设置IP地址 1:临时修改: 1.1:修改IP地址 # ifconfig eth0 192.168.2.104 1.2:修改网关地址 # route add defa ...
- QEMU命令配置虚拟机网络的用户模式
QEMU缺省使用“-net nic-net user”参数为客户机配置网络,提供了一种用户模式( user-mode)的网络模拟.使用用户模式的客户机可以连通宿主机及外部网络.用户模式网络完全由QEM ...
- Flutter Drawer 侧边栏、以及侧边栏内 容布局
Flutter Drawer 侧边栏 在 Scaffold 组件里面传入 drawer 参数可以定义左侧边栏,传入 endDrawer 可以定义右侧边 栏.侧边栏默认是隐藏的,我们可以通过手指滑动显示 ...
- OpenLDAP配置坎坷路
https://segmentfault.com/a/1190000014683418 轻型目录访问协议(英文:Lightweight Directory Access Protocol,缩写:LDA ...
- Spring cloud微服务安全实战-5-6实现授权码认证流程(2)
授权服务器,返回给我一个授权码,这里我只需要把授权传回去就可以了.来证明我是这个服务器. URI的地址传和第一次的地址一样的,认证服务器会比,第一次跳转的请求和第二次申请令牌的请求redirect_u ...
- php程序无法记录log情况下可尝试下面方法记录log
error_reporting(E_ERROR | E_PARSE); function shutdownCallback(){ $arrError = error_get_last(); // ...