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. [golang]7种 Go 程序性能分析方法

    视频信息 Seven ways to Profile Go Applicationsby Dave Cheneyat Golang UK Conf. 2016 视频:https://www.youtu ...

  2. golang-切片copy

    package main import ( "fmt" ) func main() { fmt.Println("-------------") data := ...

  3. 如何把ANSYS模型输出为CDB文件并导入FLUENT  【转载】

    转载自: http://linziok99.blog.163.com/blog/static/100157302009320134826/ 在main menu中选择Archive Model ,再点 ...

  4. SignalR了解

    一.概述 一.理解SignalR ASP.NET SignalR 是为 ASP.NET 开发人员提供的一个库,可以简化开发人员将实时 Web 功能添加到应用程序的过程. 实时 Web 功能是指这样一种 ...

  5. Debian9安装QT5.12.3

    打开虚拟机,打开火狐浏览器,输入网址下载QT5.12(linux版本,约13.G) download.qt.io/archive/qt/5.12/5.12.0/ 文件默认下载在Downloads文件夹 ...

  6. Prometheus磁盘监控

    根据挂载目录 (node_filesystem_size_bytes {mountpoint ="/"} - node_filesystem_free_bytes {mountpo ...

  7. Vue实现图片预加载

    <script>export default { data () { return { count: 0, } }, mounted: function() { this.preload( ...

  8. shell按月循环取数

    这里有个需求,按月查询,并且要输出每月的开始日期,结束日期. shell脚本如下: #!/bin/sh echo "0个参数时, 按月执行,默认当前月份 " echo " ...

  9. ArrayAdapter和ListView

    利用ArrayAdapter向ListView中添加数据 <?xml version="1.0" encoding="utf-8"?> <Li ...

  10. ISO/IEC 9899:2011 条款6.7.3——类型限定符

    6.7.3 类型限定符 语法 1.type-qualifier: const restrict volatile _Atomic 约束 2.除了指针类型(其被引用的类型是一个对象类型)之外的类型,不应 ...