【Binary Tree Right Side View 】cpp
题目:
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.
For example:
Given the following binary tree,
1 <---
/ \
2 3 <---
\ \
5 4 <---
You should return [1, 3, 4].
Credits:
Special thanks to @amrsaqr for adding this problem and creating all test cases.
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> ret;
queue<TreeNode*> curr;
queue<TreeNode*> next;
if (root) { curr.push(root); ret.push_back(root->val); }
while ( !curr.empty() )
{
while ( !curr.empty() )
{
TreeNode* tmp = curr.front();
curr.pop();
if ( tmp->left ) next.push(tmp->left);
if ( tmp->right ) next.push(tmp->right);
}
if (!next.empty()) ret.push_back(next.back()->val);
swap(next,curr);
}
return ret;
}
};
tips:
BFS算法,level order traversal binary tree
每次保留每一个level的最右一个元素的值。
【Binary Tree Right Side View 】cpp的更多相关文章
- 【Binary Tree Maximum Path Sum】cpp
题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...
- 【Binary Tree Level Order Traversal】cpp
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- 【Binary Tree Post order Traversal】cpp
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- 【Flatten Binary Tree to Linked List】cpp
题目: Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 ...
- 【遍历二叉树】04二叉树的层次遍历【Binary Tree Level Order Traversal】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的层次遍历的 ...
- 2016.6.24——vector<vector<int>>【Binary Tree Level Order Traversal】
Binary Tree Level Order Traversal 本题收获: 1.vector<vector<int>>的用法 vector<vector<int ...
- 【二叉树的递归】05二叉树中找任意起点和终点使他们的路径和最大【Binary Tree Maximum Path Sum】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,寻找值最大的路径. ...
- 【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
Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...
随机推荐
- ARM实验4—按键轮询实验
key_poll按键轮询实验 实验内容: 通过FS_4412开发板上的按键控制LED灯并打印信息. 实验目的: 熟悉开发环境的使用. 掌握猎户座4412处理器的GPIO接口, 实验平台: FS4412 ...
- python 笔记1:官网下载及安装python;eclipse中安装配置pydev
1 下载安装python. 官网:https://www.python.org/downloads/ 根据自己的操作系统选择需要的版本下载并安装. 我的电脑操作系统windows xp的,只 ...
- POJ-2376 Cleaning Shifts---区间覆盖&贪心
题目链接: https://vjudge.net/problem/POJ-2376 题目大意: farmer John要安排他的牛清理牛棚,一共有T个牛棚要清理,每头牛可以清理相邻的牛棚.比如,一头牛 ...
- 关于Ubuntu下安装Win8和Win8下安装Ubuntu的注意事项
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/svitter/article/details/32932387 本文出自:http://blog.c ...
- 你不得不掌握的thinkphp5
thinkphp官网在去年的时候发布了tp的颠覆版本thinkphp5,tp5确实比之前的版本好用了很多,增加了很多的一些特性,它采用全新的架构思想,引入了更多的PHP新特性,优化了核心,减少了依赖, ...
- 大数据(1)初始hadoop
1.hadoop模型如下: (上图为Hadoop1.x的布局) (Hadoop2.x较Hadoop1.x,多了YARN) Hadoop框架,是一个庞大的生态系统. 或者我们可以这样理解: 可以把整个体 ...
- mycat特点及用途
Mycat关键特性 关键特性 支持SQL92标准 遵守Mysql原生协议,跨语言,跨平台,跨数据库的通用中间件代理. 基于心跳的自动故障切换,支持读写分离,支持MySQL主从,以及galera clu ...
- Eclipse中文乱码解决方案
Eclipse中文乱码解决方案 1)第一个设置:window>perferences>general>workspace>text file encoding 2)Jsp编码问 ...
- linux下Tomcat配置提示权限不够解决办法
在终端输入命令 sudo chmod -R 777 /opt/Tomcat,那么Tomcat文件夹和它下面的所有子文件夹的属性都变成了777(读/写/执行权限)
- ajaxfileupload多文件上传 - 修复只支持单个文件上传的bug
搜索: jquery ajaxFileUpload AjaxFileUpload同时上传多个文件 原生的AjaxFileUpload插件是不支持多文件上传的,通过修改AjaxFileUpload少量代 ...