【Leetcode】【Medium】Binary Tree Right Side View
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的更多相关文章
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【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, ...
- 【刷题-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, ...
- 【CF438E】The Child and Binary Tree(多项式运算,生成函数)
[CF438E]The Child and Binary Tree(多项式运算,生成函数) 题面 有一个大小为\(n\)的集合\(S\) 问所有点权都在集合中,并且点权之和分别为\([0,m]\)的二 ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- [Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal
[题目] Given a binary tree, return the preordertraversal of its nodes' values. Example: Input: [1,null ...
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
随机推荐
- Java - n的阶乘计算
用递归方法,求10!的阶乘 分析: f(n) = n * f(n-1) n != 1 ----- 递推公式 f(n) = 1 ...
- decode 和 encode 区别
字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(en ...
- JS的正则表达式 - RegExp
RegExp 对象 RegExp 对象表示正则表达式,它是对字符串执行模式匹配的强大工具. 正则表达式的创建方式 1.文字格式,使用方法如下: /pattern/flags (即:/模式/标记) 2 ...
- 九度oj 1032 ZOJ 2009年浙江大学计算机及软件工程研究生机试真题
题目1032:ZOJ 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4102 解决:2277 题目描述: 读入一个字符串,字符串中包含ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出,当 ...
- 阿里云API公共参数的获取
阿里云公共参数API https://help.aliyun.com/document_detail/50284.html?spm=5176.10695662.1996646101.searchcl ...
- Opencv中图像的遍历与像素操作
Opencv中图像的遍历与像素操作 OpenCV中表示图像的数据结构是cv::Mat,Mat对象本质上是一个由数值组成的矩阵.矩阵的每一个元素代表一个像素,对于灰度图像,像素是由8位无符号数来表示(0 ...
- php发送邮件功能(PHPMailer-master插件)
当作一个插件使用即可,放到网站根目录,然后调用里面的mail.php 源码
- Markdown调查
Markdown调查 一.Editor.md 文档详细,使用者较多 1.1 主要特性 支持“标准”Markdown / CommonMark和Github风格的语法,也可变身为代码编辑器: 支持实 ...
- Effective C++ .33 子类的名称覆盖
#include <iostream> #include <cstdlib> using namespace std; class Base { public: int add ...
- Tips——单页面内的多重跳转路由使用
一.问题背景 一个路由往往代表一个地址,即一个页面.但同级网页页面的内容有很多是重复的,如果每次加载页面都要加载这些“共有”内容,会导致效率的降低.因此,单页面应用应运而生.它主张在同一页面下将“共同 ...