【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/ 题目地址 ...
随机推荐
- Android中设定背景图片平铺。
注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 在做Android开发时,我们常常需要为程序设定一个背景,但由于现在的Android设备尺寸不一,如果随便设置一个图片为背景,那么很 ...
- linux下gedit读取txt乱码解决办法
修改一下gedit的设置来让它显示的txt不再是乱码: 你可以通过以下步骤,使 gedit 正确显示中文编码文件. 按下 Alt-F2,打开“运行应用程序”对话框.在文本框中键入“gconf-edit ...
- 2013长沙 G Graph Reconstruction (Havel-Hakimi定理)
Graph Reconstruction Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Let there ...
- OC第四节——NSDictionary和NSMutableDictionary
NSDictionary 1.什么是字典 字典是也是一种集合结构,功能与我们现实中的字典工具一样 2.字典的元素是什么 任意类型的对象地址构成键值对 3. ...
- vim颜色选择+按<F9>自动编译运行+其他基本配置(ubuntu)
(以下是ubuntu上的配置........ 但如果你是在window上的,直接用一下配置吧(懒得介绍了)=.= syntax on filetype indent plugin on set rul ...
- Ubuntu 12 修改环境变量
Ubuntu Linux系统包含两类环境变量:系统环境变量和用户环境变量.系统环境变量对所有系统用户都有效,用户环境变量仅仅对当前的用户有效. 修改用户环境变量 用户环境变量通常被存储在下面的文件中: ...
- Windows上安装使用MongoDB(一)
首先下载MongoDB的Windows版本,从如下地址: https://www.mongodb.org/downloads. 我下载的msi版本,下载后安装即可,如我安装的盘符是:C:\Progra ...
- Reflow(渲染)和Repaint(重绘)
Reflow(渲染):对于DOM结构中的各个元素都有自己的盒模型,浏览器根据各种样式(浏览器的.开发人员定义的等)来计算,并根据计算结果将元素放到它该出现的位置,这个过程称之为reflow. refl ...
- Java BigDecimal详解
借用<Effactive Java>这本书中的话,float和double类型的主要设计目标是为了科学计算和工程计算.他们执行二进制浮点运算,这是为了在广域数值范围上提供 较为精确的快速近 ...
- Kali Linux渗透基础知识整理(三):漏洞利用
漏洞利用阶段利用已获得的信息和各种攻击手段实施渗透.网络应用程序漏洞诊断项目的加密通信漏洞诊断是必须执行的.顾名思义,利用漏洞,达到攻击的目的. Metasploit Framework rdeskt ...