二连水

题目地址:

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 解题报告的更多相关文章

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

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

  2. 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  3. 【LeetCode】102. Binary Tree Level Order Traversal 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目描述 Given a bi ...

  4. 【LeetCode】114. Flatten Binary Tree to Linked List 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先序遍历 递归 日期 题目地址:https://le ...

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

  6. LeetCode: Binary Tree Level Order Traversal 解题报告

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

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

  8. Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)

    Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...

  9. leetcode 199 :Binary Tree Right Side View

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

随机推荐

  1. Ogre嵌入MFC傻瓜全然教程(三)

    经过前两两篇博文的解说.我们已经完毕了渲染工作,但仅仅是渲染而没有交互性,本篇博文我们就来加上事件的处理方法. 首先我们须要为项目加入一个帧监听类:CMyFrameListener,为了直观,在这直接 ...

  2. uva 11475 - Extend to Palindrome(KMP)

    option=com_onlinejudge&Itemid=8&category=506&page=show_problem&problem=2470" ta ...

  3. (并查集)POJ 1308 & HDU 1325

    一开始以为两道题是一样的,POJ的过了直接用相同代码把HDU的交了,结果就悲剧了.最后发现HDU的没有考虑入度不能大于一. 题意:用树的定义来 判断吧,无环,n个结点最多有n-1条边,不然就会有环.只 ...

  4. CentOS下yum使用代理的设置

    export后好像没用? 问题描述: CentOS yum时出现“Could not retrieve mirrorlist http://mirrorlist.centos.org/?release ...

  5. Ray Through Glasses

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30506#problem/T 题意:给你一束光,问你在一个三层的平面类传递n次的种数: 仔 ...

  6. Android UI布局TableLayout

    了解字面上TableLayout一个表格样式布局.这种布局将包括以行和列的形式的元件被布置.表格列的数目是列的各行中的最大数目.当然,表格里面的单元格它能够清空. 实例:LayoutDemo 执行效果 ...

  7. 使用Boost库中的组件进行C++内存管理

    C++标准库中的auto_ptr,智能指针,部分的解决了获取资源自动释放的问题 在Boost中,提供了6中智能指针:scoped_ptr, scoped_array, shared_ptr, shar ...

  8. MySQL HINT:Straight_JOIN

    来自生产环境的朋友.可能都会碰到:          原本运行良好的查询语句,过了一段时间后,可能会突然变得很糟糕     一个很大可能的原因就是数据分布情况发生了变化     从而导致MySQL优化 ...

  9. IOS开发之----四舍五入问题

    方法一: -(NSString *)notRounding:(float)price afterPoint:(int)position{ NSDecimalNumberHandler* roundin ...

  10. nodejs启动守护程序pm2

    nodejs启动守护程序pm2 by 伍雪颖 做了个应用,server放阿里云,只是server总会自己断,后来写了个心跳程序,就是检測应用线程是否还在,不在就再启动, 这种方法好笨重啊,后来发现no ...