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].

解题思路:

这题和上一题Binary Tree Zigzag Level Order Traversal很相似,都需要按层遍历二叉树;

不同的是,由于Binary Tree Zigzag Level Order Traversal需要Z型遍历,需要用到“先入后出”的方法,因此用栈stack来实现;

而本题,需要每层由右向左遍历,因此用到“先进先出”,所以使用queue来实现较为方便;

需要两个queue,一个保存当前层的结点,另一个保存下一层的结点;

每层结点queue中第一个值,就是最右端值,输出这个值。

代码:

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> ret;
queue<TreeNode*> cur_layer;
cur_layer.push(root);
queue<TreeNode*> next_layer;
if (!root)
return ret; while (!cur_layer.empty()) {
ret.push_back(cur_layer.front()->val);
while (!cur_layer.empty()) {
TreeNode* node = cur_layer.front();
cur_layer.pop();
if (node->right)
next_layer.push(node->right);
if (node->left)
next_layer.push(node->left);
}
swap(cur_layer, next_layer);
} return ret;
}
};

【Leetcode】【Medium】Binary Tree Right Side View的更多相关文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  3. 【LeetCode】199. Binary Tree Right Side View

    Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...

  4. 【刷题-LeetCode】199 Binary Tree Right Side View

    Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...

  5. 【CF438E】The Child and Binary Tree(多项式运算,生成函数)

    [CF438E]The Child and Binary Tree(多项式运算,生成函数) 题面 有一个大小为\(n\)的集合\(S\) 问所有点权都在集合中,并且点权之和分别为\([0,m]\)的二 ...

  6. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  7. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  8. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  9. [Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal

    [题目] Given a binary tree, return the preordertraversal of its nodes' values. Example: Input: [1,null ...

  10. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

随机推荐

  1. Python批量复制迁移文件夹

    前言 Python可以利用shutil库进行对文件夹,文件的迁移.而在本次的实践当中,难点在于目标文件夹的名称和数据源文件夹的名称,需要利用 工作单位提供的中间数据去进行对比连接起来. 例如:目标源的 ...

  2. Android中判断service是否在运行

    /** * 判断服务是否开启 * * @return */ public static boolean isServiceRunning(Context context, String Service ...

  3. (转)mysql 5.6 原生Online DDL解析

    做MySQL的都知道,数据库操作里面,DDL操作(比如CREATE,DROP,ALTER等)代价是非常高的,特别是在单表上千万的情况下,加个索引或改个列类型,就有可能堵塞整个表的读写. 然后 mysq ...

  4. sencha touch overlay 里使用 list

    1 sencha touch 中 list 如果不设置一个固定高度或 flex : 1,  list 的内容就不会显示出来. 主要是因为 list 是可滚动的,你不设置高度 ,list 的高度默认就是 ...

  5. FFmpeg的H.264解码器源代码简单分析

    本文简单记录FFmpeg中libavcodec的H.264解码器(H.264 Decoder)的源代码.这个H.264解码器十分重要,可以说FFmpeg项目今天可以几乎“垄断”视音频编解码技术,很大一 ...

  6. Android 开发 命名规范(基础回顾)

    标识符命名法标识符命名法最要有四种: 1 驼峰(Camel)命名法:又称小驼峰命名法,除首单词外,其余所有单词的第一个字母大写. 2 帕斯卡(pascal)命名法:又称大驼峰命名法,所有单词的第一个字 ...

  7. Linux下实现MySQL数据库自动备份

    1.给mysql创建用户备份的角色,并且授予角色SELECT, RELOAD, SHOW DATABASES, LOCK TABLES等权限. mysql> create user 'backu ...

  8. 使用js命名空间进行模块式开发

    在java中,为了防止命名冲突和模块式开发,会有个一个import 关键字来进行导包. 在js中为了达到同样的效果,我们也通过给其自定义一个“包”的概念. 直接上代码: 首先有个LC.js文件: // ...

  9. OSI与TCP/IP协议区别

    1 OSI参考模型 谈到网络不能不谈OSI参考模型,虽然OSI参考模型的实际应用意义不是很大,但其的确对于理解网络协议内部的运作很有帮助,也为我们学习网络协议提供了一个很好的参考.在现实网络世界里,T ...

  10. 批量修改dos文件到unix

    1. 安装dos2unix 2. 执行:find ./ -type f | xargs dos2unix