【LeetCode-面试算法经典-Java实现】【199-Binary Tree Right Side View(从右边看二叉树)】
【199-Binary Tree Right Side View(从右边看二叉树】
【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】
代码下载【https://github.com/Wang-Jun-Chao】
原题
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].
题目大意
给定一个二叉树,想象自己站在树的右边,返回从下到下你能看到的节点的值。
解题思路
二叉树的层次遍历。每层依照从左向右的顺序依次訪问节点,(每一层取最右边的结点)
代码实现
树结点类
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
算法实现类
public class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new LinkedList<>();
if (root != null) {
Deque<TreeNode> deque = new LinkedList<>();
// 当前层的结点数
int current = 1;
// 下一层的结点数
int next = 0;
TreeNode node;
deque.addLast(root);
while (deque.size() > 0) {
// 取第一个结点
node = deque.removeFirst();
current--;
// 加入非空的左结点
if (node.left != null) {
next++;
deque.addLast(node.left);
}
// 加入非空的右结点
if (node.right != null) {
next++;
deque.addLast(node.right);
}
// 假设当前层已经处理完了
if (current == 0) {
// 保存此层的最右一个结点值
result.add(node.val);
// 设置下一层的元素个数
current = next;
next = 0;
}
}
}
return result;
}
}
评測结果
点击图片,鼠标不释放,拖动一段位置。释放后在新的窗体中查看完整图片。
特别说明
欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47970785】
【LeetCode-面试算法经典-Java实现】【199-Binary Tree Right Side View(从右边看二叉树)】的更多相关文章
- 199. Binary Tree Right Side View 从右侧看的节点数
[抄题]: Given a binary tree, imagine yourself standing on the right side of it, return the values of t ...
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. 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】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, ...
- Java for LeetCode 199 Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- [LeetCode] 199. Binary Tree Right Side View 二叉树的右侧视图
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- leetcode@ [199] Binary Tree Right Side View (DFS/BFS)
https://leetcode.com/problems/binary-tree-right-side-view/ Given a binary tree, imagine yourself sta ...
- LeetCode OJ 199. Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
随机推荐
- 紫书 习题 10-20 UVa 1648 (推公式)
设一次上去a层,一次下去b层,有x次上去,有(n-x)次下去 则ax - (n-x)b >= 1 x >= (nb+1) / (a+b) 如果可以整除, x = (nb+1) / (a+b ...
- 【Codeforces Round #425 (Div. 2) A】Sasha and Sticks
[Link]: [Description] [Solution] 傻逼题; 获取n/k; 对n/k的奇偶性讨论一下就好 [NumberOf WA] 0 [Reviw] [Code] #include ...
- HDU4596 Yet another end of the world 扩展欧几里德性质
这题坑了,我真该吃翔啊,竟然一開始方程设错了并且没有去想连列的问题,我真是坑货,做不出就该又一次理一下嘛.操蛋. 题意:给了N组x,y,z然后 问你是否存在两个或者两个以上的id,是的 id%x的值在 ...
- Java的线程机制
一.Java中实现多线程的两种方式1) 继承Thread类 Thread类包括了包括和创建线程所需的一切东西. Thread 最重要的方法是 run().编写线程程序时须要覆盖 run() 方法,ru ...
- FFmpeg的HEVC解码器源码简单分析:解码器主干部分
===================================================== HEVC源码分析文章列表: [解码 -libavcodec HEVC 解码器] FFmpeg ...
- 归并排序_分治算法 (白书P226)
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int a ...
- Extjs在HtmlEditor的工具栏上插入自定义按钮
Ext.ns('Ext.ux.form.HtmlEditor');Ext.ux.form.HtmlEditor.HR =Ext.extend(Ext.util.Observable,{ init:fu ...
- CentOS下编译安装Apache
与Apache 2.2.x相比,Apache 2.4.x提供了很多性能方面的提升,包括支持更大流量.更好地支持云计算.利用更少的内存处理更多的并发等.除此之外,还包括性能提升.内存利用.异步 I/O的 ...
- ubuntu下eclipse java ee首次打开提示找不到jdk的问题
最近想搭建一个本地服务器,方便写一些网络请求相关的demo,遂下载了eclipse ee版 ( IDEA证书好贵,暂时不想买-=-),下载---解压 一切正常,但是当在terminal下打开ecli ...
- 分享js寄生组合模式继承
function person(){ this.name = 'taobao'; this.showMess = function(){ return this.name; } } person.pr ...