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. ACM_1 大数求和

    /*1 *2014.11.18 *大数求和 */ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <strin ...

  2. jQuery框架分析第一章: 第一个匿名函数

    我的jQuery版本为1.7* 这个版本代码比之前的版本优化了很多,结构也清晰了不少,就用最新的吧. 打开jQuery源代码 首先你能看到所有代码被一个 (function(window,undefi ...

  3. Java多线程基础知识(二)

    一. Java线程具有6种状态  NEW 初始状态,线程被创建,但是还没有调用start方法. RUNNABLE 运行状态,java线程将操作系统中的就绪和运行两种状态笼统的称作进行中. BLOCKE ...

  4. Java中Runnable和Thread

    java中有两种实现多线程的方式:一种是继承Thread类,一种是实现Runnable接口. 1.java启动线程为什么使用start函数呢? 在JDK的安装路径下,src.zip是全部的java源程 ...

  5. 跟着百度学PHP[4]OOP面对对象编程-17-多态

    多态是除封装和继承之外的另一个面象对象的三大特性之一. 多态的作用简而言之就是为程序做括展. 比如说在公司上班,每个月财务发放工资,同一个发工资的方法,在公司内不同的员工或是不同职位的员工,都是通过这 ...

  6. Ubuntu+Apache+PHP+Mysql环境搭建

    一.操作系统Ubuntu 14.04 64位,虚拟机服务器 二.Apache 1.安装Apache,安装命令:sudo apt-get install apache2 2.环境配置: 1)配置文件:路 ...

  7. OLA音频变速算法的仿真与剖析

    前段时间,在尝试音乐节拍数的提取时,终于有了突破性的进展,效果基本上比市面上的许多商业软件还要好,在作节拍数检测时,高频信息作用不大, 通过重采样减小运算量.重采样让我想起了在学校里面做的变速变调算法 ...

  8. c语言——gcc,make,vim

    在linux环境下进行c语言编程,基础的工具就是gcc和make.关于这2个工具的内容有很多,现在要做的仅仅是起步. gcc的用法: -S Compile only; do not assemble ...

  9. Frog Jump

    A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...

  10. Verify Preorder/Inorder/Postorder Sequence in Binary Search Tree

    Verify Preorder Sequence in Binary Search Tree \Given an array of numbers, verify whether it is the ...