Leetcode199. Binary Tree Right Side View二叉树的右视图
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
示例:
输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释:
先求深度,中序遍历或者前序遍历都可以
class Solution {
public:
vector<int> v;
vector<int> rightSideView(TreeNode* root)
{
int len = GetDepth(root);
if(len == 0)
return v;
v = vector<int>(len , 0);
GetAns(root, 0);
return v;
}
void GetAns(TreeNode* root, int depth)
{
if(root == NULL)
return;
v[depth] = root ->val;
GetAns(root ->left, depth + 1);
GetAns(root ->right, depth + 1);
}
int GetDepth(TreeNode *root)
{
if(root == NULL)
return 0;
return 1 + max(GetDepth(root ->left), GetDepth(root ->right));
}
};
Leetcode199. Binary Tree Right Side View二叉树的右视图的更多相关文章
- 199 Binary Tree Right Side View 二叉树的右视图
给定一棵二叉树,想象自己站在它的右侧,返回从顶部到底部看到的节点值.例如:给定以下二叉树, 1 <--- / \2 3 <--- \ ...
- [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 nod ...
- [LeetCode] Binary Tree Right Side View 二叉树的右侧视图
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- [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 nod ...
- [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 nod ...
- LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Giv ...
- Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)
Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
随机推荐
- selenium基础(获取验证信息-断言)
获取验证信息 实际结果与预期结果进行比较称之为断言 通过获取title.URL.text等信息进行断言 text方法用于获取标签对之间的文本信息 from selenium import webdri ...
- oracle 如何在一个数据库创建多个实例
实例:是一个非固定的.基于内存的基本进程与内存结构.当服务器关闭后,实例也就不存在了. 数据库(Database)指的是固定的.基于磁盘的数据文件.控制文件.日志文件.参数文件和归档日志文件等. 一般 ...
- eclipse-帮助文档
Eclipse开发环境配置 1. java环境 安装 本系统使用java6开发,老师使用1.6.0 _45版本开发,如下图所示: “开发工具”目录提供了1.6.0 _45版本32位和6 ...
- Ubuntu环境下Error: Invalid or corrupt jarfile xxx.jar
一.问题描述 Ubuntu环境下将Maven项目打包成jar包后,运行一下指令: $ java -jar my.jar 发生错误: Error: Invalid or corrupt jarfile ...
- WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel
关于这个异常的问题网上有很多的解决方案. 最为靠谱的有: http://www.cnblogs.com/hjf1223/archive/2007/03/14/674502.html(若因为链接而导致不 ...
- go的单引号、双引号、反引号的区别
Go语言的字符串类型string在本质上就与其他语言的字符串类型不同: Java的String.C++的std::string以及Python3的str类型都只是定宽字符序列 Go语言的字符串是一个用 ...
- zabbix自动发现主机(转)
zabbix自动发现主机 2018年06月15日 18:02:52 loyal-Wang 阅读数:817更多 个人分类: zabbix 版权声明:本文为博主原创文章,转载请注明出处. https: ...
- php中date() 函数
实例 格式化本地日期和时间,并返回格式化的日期字符串: <?php // Prints the day echo date("l") . "<br>&q ...
- 菜鸟安装 CocoaPods
在 iOS 项目开发中,经常会用到第三方的源代码,CocoaPods 就是为了方便管理这些源码的工具. 在官方教程里面,安装看起来非常简单 $ [sudo] gem install cocoapods ...
- 力扣算法题—146LRU缓存机制
[题目] 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - 如果密钥 (k ...