给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。

示例:

输入: [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二叉树的右视图的更多相关文章

  1. 199 Binary Tree Right Side View 二叉树的右视图

    给定一棵二叉树,想象自己站在它的右侧,返回从顶部到底部看到的节点值.例如:给定以下二叉树,   1            <--- /   \2     3         <--- \  ...

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

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

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

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

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

    199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Giv ...

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

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

  8. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  9. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

随机推荐

  1. PagedListCore的使用

    关于在Core2.0中PagedListCore实现分页 一.引言 开发中在对大量数据展示的时候,出于对性能的考虑,我们往往是使用分页功能(用户需要那一页我们就返回给他那一页,而不是一次性加载所有的数 ...

  2. PyInstaller打包Python源文件为可执行程序exe

    1. 安装PyInstaller 使用命令:pip install PyInstaller时可能会由于网络的问题出现以下问题: pip._vendor.urllib3.exceptions.ReadT ...

  3. iOS逆向系列-theos

    概述 theos是GitHub开源的一个项目,通过nic.pl创建tweak项目.通过编写我们注入代码,然后执行编译.打包.安装等操作将代码注入iPhone安装的制定程序. theos环境配置 安装签 ...

  4. iOS开发系列-JSON解析

    概述 JOSN是一种轻量级的数据格式,一般用于数据交互.服务器返回给客户端,一般都是JSON格式或者XML格式. JSON的格式: {"name" : "CoderHon ...

  5. Python---求100以内的质数

    1.首先什么是质数: 一个大于1的正整数,如果除了1和它本身以外,不能被其他正整数整除,就叫质数,也叫素数.如2,3,5,7,11,13,17…. 2.代码如下: 这里做个解析:①Python的for ...

  6. [JZOJ3187]【GDOI2013模拟8】的士

    题目 描述 题目大意 在一个数轴上,有些人要从某个点到达另一个点. 出租车从最左端出发,将所有人送到它们的目的地,最终到达最右边的点. 出租车只能做一个乘客,并且可以在图中将乘客丢下. 问最短时间. ...

  7. 2016.9.10初中部上午NOIP普及组比赛总结

    2016.9.10初中部上午NOIP普及组比赛总结 链接:https://jzoj.net/junior/#contest/home/1340 好不爽!翻车了!不过排名差不多在中间偏上一点, 还好不是 ...

  8. Django之深入了解路由层

    目录 ORM表关系建立 一对一 一对多 多对多 Django 请求生命周期 url 路由层 路由匹配 无名分组 有名分组 反向解析 路由匹配条件无分组的情况的反向解析 无名分组情况的反向解析 有名分组 ...

  9. Failed to load resource: net::ERR_INSECURE_RESPONSE 问题解决记录

    项目在小米自带浏览器中出现了文件丢失.经检查发现这些链接引用全部是完整的线上url.改为相对路径问题解决. 同时消失的bug还有一个Error in event handler for runtime ...

  10. js 数据绑定

    //   回流:(重排 reflow) 当HTML的DOM结构(删除.增加.位置等)发生改变时引起DOM回流.浏览器重新计算DOM结构,重新的对当前DOM结构进行渲染 //   重绘:某一个元素的部分 ...