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,2,3,null,5,null,4]
输出: [1, 3, 4]
解释:
```
1

The core idea of this algorithm:

1. Each depth of the tree only select one node.
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);
}
}

相似题目

参考资料

LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)的更多相关文章

  1. [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 ...

  2. Java实现 LeetCode 199 二叉树的右视图

    199. 二叉树的右视图 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, ...

  3. 力扣Leetcode 199. 二叉树的右视图

    199. 二叉树的右视图 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, ...

  4. LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)

    题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释: 1 ...

  5. leetcode.199二叉树的右视图

    给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4]输出: [1, 3, 4]解释: 1 <-- ...

  6. LeetCode 199 二叉树的右视图

    题目: 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释: 1 ...

  7. LeetCode 199. 二叉树的右视图 C++ 用时超100%

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  8. LeetCode——199. 二叉树的右视图

    给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释: 1 < ...

  9. Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)

    Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...

随机推荐

  1. Xshell6如何传输文件

    Xshell6如何传输文件  /或者直接在本地用notepad  nftp插件上传本地文件,直观,更方便 上传文件 1.打开xshell6软件,连接服务器. 2.yum安装一款工具.#yum inst ...

  2. 数据量与半监督与监督学习 Data amount and semi-supervised and supervised learning

    机器学习工程师最熟悉的设置之一是访问大量数据,但需要适度的资源来注释它.处于困境的每个人最终都会经历逻辑步骤,当他们拥有有限的监督数据时会问自己该做什么,但很多未标记的数据,以及文献似乎都有一个现成的 ...

  3. Luogu5349 幂

    题面 题解 首先考虑对于一个单项式怎么做,多项式就是单项式的答案的和. 就求一下\(\mathbf f(n) = n^k\)吧.(下面设\(t = \dfrac 1r\)) 设\(\mathbf S_ ...

  4. NAT端口映射综合详解

  5. 分布式缓存Redis集群搭建

    redis安装 1.下载tar包至/opt/redis 2.解压tar包 tar -xvf redis-4.0.14.tar.gz 3. cd redis-4.0.14 make一下. 单节点的red ...

  6. Kali Linux硬盘扩容

    传送门--->http://www.kali.org.cn/thread-27079-1-1.html.kali虚拟机扩容

  7. 值得H5前端学习的60个JS插件(含DEMO演示)

    下面也可以说是H5前端学习的js插件大全.基本包含了大部分的前端最前沿的js插件和库. 布局 SuperEmbed.js- 是一个Javascript库,可检测出网页上的内嵌视频并使他们能够变成响应式 ...

  8. 范仁义web前端介绍课程---1、课程意义

    范仁义web前端介绍课程---1.课程意义 一.总结 一句话总结: 提供的这一整套学习方法和资源,配合艾宾浩斯遗忘曲线等各种复习.学习算法和后续会有的娱乐化学习方式,能否真正做到让学过的东西不再忘记. ...

  9. vim 替换所有字符串

    将dev 替换 为 prod: :%s/dev/prod/g

  10. 树莓派 pip 手动安装 和使用阿里云源

    在 /etc/pip.conf 添加如下内容,启用源 [global] extra-index-url=https://www.piwheels.org/simple 下载后 pip install ...