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) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...
随机推荐
- UOJ#470. 【ZJOI2019】语言 虚树,线段树合并
原文链接www.cnblogs.com/zhouzhendong/p/UOJ470.html 前言 做完情报中心来看这个题突然发现两题有相似之处然后就会做了. 题解 首先,我们考虑将所有答案点对分为两 ...
- goland 安装破解
链接:https://pan.baidu.com/s/1vH70CHq122RbfwLwbHewjg 密码:zilv 复制如下注册码: 56ZS5PQ1RF-eyJsaWNlbnNlSWQiOiI1 ...
- ICEM-八分之一球体
原视频下载链接:https://yunpan.cn/c6seunjgbwRhJ 访问密码 c780
- LeetCode 第 159 场周赛
一. 缀点成线(LeetCode-5230) 1.1 题目描述 1.2 解题思路 比较简单的一题,利用公式 y = kx + b,确定好k和b就好,并且要考虑一种情况,函数 x = h. 1.3 解题 ...
- [转载]运行中的DLL自升级
最近手头有个需求:dll需要注入到某个进程常驻,该dll具备自我升级能力,当发现新的可用版本时,立即Free自己,加载新的.下面是一个实现方案: 开启一个监听线程,从网络上拉新的可用版本,下载放到 ...
- JS实现文字向上无缝滚动轮播
效果图: 全部代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- 【Java.Regex】用正则表达式查找Java源文件中的注释
代码: package regex; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.I ...
- excel自增编号
1.首先在单元格 A2:C2 中输入数据. 2.选择 A3:A500, 我们按键盘的 CTRL G 键, 拉起定位框, 然后在参考位置, 输入 A3:A500, 然后单击 "确定" ...
- OpenJudge 计算概论1007:点评赛车
总时间限制: 1000ms 内存限制: 65536kB描述4名专家对4款赛车进行评论1)A说:2号赛车是最好的:2)B说:4号赛车是最好的:3)C说:3号赛车不是最好的:4)D说: B说错了.事实上只 ...
- Java基础 if if-else if-else if-else 三种示例
JDK :OpenJDK-11 OS :CentOS 7.6.1810 IDE :Eclipse 2019‑03 typesetting :Markdown code ...