leetcode 199. Binary Tree Right Side View
2016-03-04 16:05
225 查看
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,
You should return
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int>re;
if(root==NULL)
return re;
vector<TreeNode*>quene;
quene.push_back(root);
while(!quene.empty())
{
re.push_back(quene.back()->val);
vector<TreeNode*>newquene;
for(int i=0;i<quene.size();i++)
{
TreeNode*node=quene[i];
if(node->left!=NULL)
newquene.push_back(node->left);
if(node->right!=NULL)
newquene.push_back(node->right);
}
quene=newquene;
}
return re;
}
};
accepted
For example:
Given the following binary tree,
1 <--- / \ 2 3 <--- \ \ 5 4 <---
You should return
[1, 3, 4].
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int>re;
if(root==NULL)
return re;
vector<TreeNode*>quene;
quene.push_back(root);
while(!quene.empty())
{
re.push_back(quene.back()->val);
vector<TreeNode*>newquene;
for(int i=0;i<quene.size();i++)
{
TreeNode*node=quene[i];
if(node->left!=NULL)
newquene.push_back(node->left);
if(node->right!=NULL)
newquene.push_back(node->right);
}
quene=newquene;
}
return re;
}
};
accepted
相关文章推荐
- Unity多人游戏和网络功能(一) 概述和基本概念
- Ipython qtconsole已经没法用了
- 【VS开发】fopen 文本文件与二进制文件区别
- 安装Git
- “大数据”基础知识普及
- linux基础指令二
- 大话设计模式-原型模式
- 1008: 数列
- Android之 ListActivity 的使用和在 ListView中添加图片文字
- Struts2基础(一个简单的小例子)
- Getting Started with ASP.NET Web API 2 (C#)
- iOS中如何隐藏启动图片的状态栏
- 匈牙利算法
- JQuery增删改查
- 金牛VS天蝎
- 【笔记】《C#大学教程》- 第5章 控制结构(二)
- MvvmLight框架使用入门(一)
- bzoj1016【最小生成树计数】
- JSON与JAVA数据的转换
- log4j的作用