【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 ...
随机推荐
- 数据备份及恢复(mongodump/mongorestore)
说明 1.mongodump创建高保真的BSON文件,mongorestore可以用其恢复数据库.对于小型数据库的备份和恢复,这两个工具非常简单和高效,但对于大型数据库的备份并不理想.2.mongod ...
- n皇后问题(分析)
这道题需要用到回溯算法,现在在这里先简单的介绍一下这个算法: 回溯算法也叫试探法,它是一种系统地搜索问题的解的方法.回溯算法的基本思想是:从一条路往前走,能进则进,不能进则退回来,换一条路再试.用回溯 ...
- Python基础(2) - 动态数据类型
Python是一门强类型语言,单定义变量时不需要制定类型. C#这样定义变量: ; VB这样定义变量: Python不需要制定类型,给变量赋什么类型的值,它就是什么类型.(穿神马就是神马?) > ...
- [Git & GitHub] 利用Git Bash进行第一次提交文件
转载:https://blog.csdn.net/dietime1943/article/details/72420042 利用Git Bash进行第一次提交文件 快下班的时候,MD群里有人问怎么向g ...
- git submodule的使用
1.在项目中使用Submodule 为当前工程添加submodule,命令如下:git submodule add 仓库地址 路径仓库地址:是指子模块仓库地址URL.路径:指将子模块放置在当前工程下的 ...
- UILable 标题加粗代码
UILable 标题加粗代码: 加粗; [UILabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]]; 加粗并 ...
- java中多线程中测试某个条件的变化用 if 还是用 while?
最近在研究wait和notify方法,发现有个地方要注意,但是网上又说得不是很明白的地方,就是经典的生产者和消费模式,使用wait和notify实现,判断list是否为空的这个为什么要用while而不 ...
- Freemarker 最简单的例子程序
首先导入包,freemarker.jar 下载地址: freemarker-2.3.18.tar.gz http://cdnetworks-kr-1.dl.sourceforge.net/pro ...
- .net core 第一篇选择开发工具和环境
.net core 已经发布三年了,社区也逐步成熟.作为微软阵营的一员,忙了一年年底抽点时间系统学习下.学习资料主要为以下为主: 1. https://docs.microsoft.com/zh-cn ...
- 在ASP.NET CORE中启用favicon.ico
在静态页面中添加网站标志只需在<head>标签中添加<link rel="shortcut icon" href="favicon.ico" ...