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. mybatis批量插入数据到oracle

    mybatis 批量插入数据到oracle报 ”java.sql.SQLException: ORA-00933: SQL 命令未正确结束“  错误解决方法 oracle批量插入使用 insert a ...

  2. 两个伪类下特有的属性 content

    更加具体的可以参考:https://developer.mozilla.org/zh-CN/docs/Web/CSS/content

  3. 基于Windows10安装Ubuntu双系统

    步骤: 1.从Ubuntu的官网上下载Ubuntu的iSO安装包. http://www.ubuntu.com/download/ 我安装的版本是Ubuntu 14.04.3 LTS 64位版本 2. ...

  4. 01knockout应用开发之遍历简单数据$Index、$data

    在knockout环境下,如何遍历一个简单的数组?对于遍历对象组件的数组来说,很容易,直接foreach:对象名,然后进行属性的绑定即可,而如下数据[10,20,30]这种简单的数组,如何去遍历呢?在 ...

  5. BZOJ3052——糖果公园

    0.题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3052 1.题目大意:给定一颗n个点的无根树,每个点有一个颜色,要进行q次操作,有两种操 ...

  6. hiho一下 第一百零七周 Give My Text Back(微软笔试题)

    题目1 : Give My Text Back 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 To prepare for the English exam Littl ...

  7. ARPACK在window visual Studio的安装配置

    ARPACK是一个求解大规模稠密/稀疏矩阵问题的库,最近在做特征值问题时用到.ARPACK这库相当古老,最早是RICE的一帮人弄的.LAPACK也差不多,貌似是美帝某个.gov发起的.这俩源代码是Fo ...

  8. PHPCMS V9教程之快速入门

    这篇文章要为大家来介绍PHPCMS V9这个系统的一些基本知识,PHPCMS是基于面向对象的,严格的安装MVC开发模式开发的CMS系统,同时他还是一个非 常不错的PHP框架.下面我们一起看一下PHPC ...

  9. 【smarty项目源码】模拟smarty模版文件的解析过程

    <?php class MyMiniSmarty{ //模版文件的存放路径 var $template_dir="./templates/"; //编译文件的存放路径 ,编译 ...

  10. ASP.NET 上的 Async/Await 简介

    原文链接 大多数有关 async/await 的在线资源假定您正在开发客户端应用程序,但在服务器上有 async 的位置吗?可以非常肯定地回答“有”.本文是对 ASP.NET 上异步请求的概念性概述, ...