【原创】leetCodeOj --- Binary Tree Right Side View 解题报告
二连水
题目地址:
https://leetcode.com/problems/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]
.
Credits:
Special thanks to @amrsaqr for adding this problem and creating all test cases.
题目解析:
乍一看,酷炫异常,实际上单纯得让人想哭。
让你找到二叉树每层的最后一个元素。
怎么早?
BFS一层,队列的最后一个节点。
具体代码:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> rightSideView(TreeNode root) {
Deque<TreeNode> bfs = new LinkedList<TreeNode>();
List<Integer> result = new ArrayList<Integer>();
if (root == null) {
return result;
}
bfs.add(root);
while (!bfs.isEmpty()) {
TreeNode last = bfs.getLast();
result.add(last.val);
TreeNode first = bfs.getFirst();
while (first != last) {
if (first.left != null) {
bfs.add(first.left);
}
if (first.right != null) {
bfs.add(first.right);
}
bfs.pop();
first = bfs.getFirst();
}
// handle last
if (last.left != null) {
bfs.add(last.left);
}
if (last.right != null) {
bfs.add(last.right);
}
bfs.pop();
}
return result;
}
}
【原创】leetCodeOj --- Binary Tree Right Side View 解题报告的更多相关文章
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】102. Binary Tree Level Order Traversal 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目描述 Given a bi ...
- 【LeetCode】114. Flatten Binary Tree to Linked List 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先序遍历 递归 日期 题目地址:https://le ...
- Leetcode:Flatten Binary Tree to Linked List 解题报告
Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ex ...
- LeetCode: Binary Tree Level Order Traversal 解题报告
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- LeetCode: Binary Tree Maximum Path Sum 解题报告
Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and e ...
- Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)
Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
随机推荐
- AMR音频编码器概述及文件格式分析
全称Adaptive Multi-Rate,自适应多速率编码,主要用于移动设备的音频,压缩比比较大,但相对其他的压缩格式质量比较差,由于多用于人声,通话,效果还是很不错的. 一.分类 1. AMR: ...
- sqlserver 存储过程学习笔记(二) 在项目中的应用<多表分页>
(1)存储过程建立 USE [NewPlat] GO /****** Object: StoredProcedure [dbo].[usp_PagingLarge] Script Date: ...
- delphi 回调函数
program Project2; {$APPTYPE CONSOLE} uses SysUtils; type //定义一个对象事件方法 TCallbackFunc = function (i: I ...
- QTableView 固定列宽度(鼠标拖动后,仍可固定)
QTableView 提供一个函数: void QTableView::setColumnWidth ( int column, int width ) 用于设置column指定的列的宽度 但setC ...
- 理解Spring的Bean工厂
一提到工厂,我们先来回顾前面学习过的工厂方法和抽象工厂模式: 工厂方法:针对产品维度,能够产生新的产品,也能够产生新的产品工厂,既能够扩展产品维度.可是假设我们想在普通工厂上生产产品系列,就会特别麻烦 ...
- The tempfile module
The tempfile module The tempfile module This module allows you to quickly come up with unique names ...
- 各种oracle10g下载地址(官网网页上好像找不到了)
Oracle Database 10g Release 2 (10.2.0.1.0) Enterprise/Standard Edition for Microsoft Windows (32-bit ...
- iptables 小结
最近工作上一个作业用到了iptables命令,主要进行端口映射,在网上查了好多资料,尽管有很多例子,但还是整了好几天才整明白.在这里将学习到的知识总结一下.以下均属个人见解.(有一些是从网络中总结的, ...
- tomcat 下部署 php
由于需要测试一个PHP的环境.故记录此处. 环境 OS:win8.1 up1 64bit tomcat :8.0.14 64bit php:php-5.6.2-Win32-VC11-x64.zip 将 ...
- POJ 2676/2918 数独(dfs)
思路:记录每行每列每一个宫已经出现的数字就可以.数据比較弱 另外POJ 3074 3076 必须用剪枝策略.但实现较麻烦,还是以后学了DLX再来做吧 //Accepted 160K 0MS #incl ...