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)的更多相关文章

  1. 【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 ...

  2. 【leetcode】Pascal's Triangle I & II (middle)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  3. 【leetcode】Bitwise AND of Numbers Range(middle)

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  4. 【leetcode】Longest Substring Without Repeating Characters (middle)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  5. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  6. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  7. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  8. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  9. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

随机推荐

  1. godaddy空间的sql server数据库没办法insert中文

    原来的代码: use string007 from sysobjects where id = object_id('K_V_TEST') and type = 'U') drop table K_V ...

  2. 基础知识系列☞各版本下IIS请求处理过程区别

    转载地址→http://www.cnblogs.com/fsjohnhuang/articles/2332074.html ASP.NET是一个非常强大的构建Web应用的平台, 它提供了极大的灵活性和 ...

  3. HDOJ 2929 Bigger is Better

    DP....好难的DP... Bigger is Better Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  4. Linux下安装和配置JDK与Tomcat(入门版)

    JDK路径:/usr/java/jdk1.6.0_25 Tomcat路径:/usr/local/apache-tomcat 1. 下载jdk6.0(选择“.rpm.bin”结尾的,6u25版本) ht ...

  5. PHPCMS V9多站点[站群功能]动态设置与静态设置子站内容URL

    今天我们来讲解下 PHPCMS V9的站群功能的 动态站点与静态站点的配置 站群站点,分为动态站点,和静态站点两种设置方法: 静态的,就是将栏目和内容都了HTML 文件,我们先讲解下,站群的操作: 建 ...

  6. vue.js 简单入门

    转载自:http://blog.csdn.net/violetjack0808/article/details/51451672 <!DOCTYPE html> <html lang ...

  7. 8-IO总结

    3. 4. 5.

  8. SQL Server 跨数据库查询

    语句 SELECT * FROM 数据库A.dbo.表A a, 数据库B.dbo.表B b WHERE a.field=b.field "DBO"可以省略 如 SELECT * F ...

  9. Vim编辑器运用的五个技巧

    导读 如今 Vim 是每个人最喜欢的 Linux 文本编辑器,也是开发者和系统管理者最喜爱的开源工具.大多数人只是熟悉Vim的最最基本的操作,只能在终端使用 Vim 修改文本,但是它并没有任何一个我想 ...

  10. Xcode 中的黄色文件夹/蓝色文件夹

    蓝色.黄色首先是和你导入文件夹时的勾选项目有关系: 黄色:-->group 蓝色:--> folder 在group中的.m/.h文件,#import "xxxxx.h" ...