【leetcode】Binary Tree Right Side View(middle)
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]
.
思路:BFS
class Solution {
public:
vector<int> rightSideView(TreeNode *root) {
vector<int> ans;
if(root == NULL) return ans;
queue<TreeNode *> Q;
Q.push(root); while(!Q.empty())
{
ans.push_back(Q.front()->val);
int pos = Q.size(); //当前层的元素个数
while(pos != )
{
if(Q.front()->right != NULL)
Q.push(Q.front()->right);
if(Q.front()->left != NULL)
Q.push(Q.front()->left);
Q.pop();
pos--;
}
} return ans;
}
};
【leetcode】Binary Tree Right Side View(middle)的更多相关文章
- 【leetcode】House Robber & House Robber II(middle)
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- 【leetcode】Pascal's Triangle I & II (middle)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 【leetcode】Bitwise AND of Numbers Range(middle)
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- 【leetcode】Longest Substring Without Repeating Characters (middle)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
随机推荐
- MySql的安装及配置详细指引!
一.安装My Sql数据库 1.1,首先下载MySQL与HeidiSQL工具,双击打开后可以看到名为”mysql-5.0.22-win32 Setup.exe”的安装程序,双击执行该程序. 1.2,打 ...
- 快速切换IP的批处理!
内容如下: @echo off color 1A Title [SMART专用 IP设置V1.0] cls echo. echo SMART专用 IP设置V1.0 %date%%time% echo. ...
- HDOJ 1561 The more, The Better
树形DP.... The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 2015多校.Zero Escape (dp减枝 && 滚动数组)
Zero Escape Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- LUXURY 8
A - Gargari and Bishops Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- Hadoop 之MongoDB
NoSql 简介: COUCH DB REDIS MONGODB NEO4J HBASE BIGTABLE 存储非结构化数据 索引多:单键,多键,数组,全文,2D. MonggoDB数据类型: nul ...
- mysql python image
连接mysql数据库: cnx = mysql.connector.connect(user='joe', database='test') Connector/Python参数列表 Argument ...
- JAVA操作Oracle数据库中的事务
实验1: create table yggz(code int, salary number(7,2)); insert into yggz values(1, 1000); insert into ...
- 随机添加一个Class,Class提前写好
$("").hover(function(){ var ary = ["red","green","blue",]; v ...
- Codeforces Gym 100114 D. Selection
Description 问选择一个序列上的所有数的最少操作次数,跟电脑上选择文件一样,输出操作方案. Sol 贪心. 用Shift一段文件只能使用一次,之后必须一直按Ctrl了. 然后就是看用Shif ...