199. 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].
其实就是层序遍历 的每层的最后一个元素!!!!
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
Queue<TreeNode> queue = new LinkedList<TreeNode>();
if(root==null) return res;
queue.offer(root);
int level_num = 1;
while (!queue.isEmpty()) {
level_num = queue.size();
for(int i = 0; i < level_num; i++){
TreeNode node = queue.poll();
if(i==level_num-1)
res.add(node.val);
if(node.left != null) queue.offer(node.left);
if(node.right != null) queue.offer(node.right);
}
}
return res;
}
}
199. Binary Tree Right Side View -----层序遍历的更多相关文章
- 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
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- 【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, ...
- [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之Binary Tree Level Order Traversal 层序遍历二叉树
Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...
- 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 ...
- 102. Binary Tree Level Order Traversal ------层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to righ ...
随机推荐
- mogndb 慢查询
0 摘要 在MySQL中,慢查询日志是经常作为我们优化查询的依据,那在MongoDB中是否有类似的功能呢?答案是肯定的,那就是开启Profiling功能.该工具在运行的实例上收集有关MongoDB的 ...
- jsp页面定义的map
<script type="text/javascript">function Map(){ this.elements = new Array(); //删除MAP所 ...
- windows下搭建ffmpeg+nginx+rtmp-module搭建实时视频环境
下载ffmpeg的Windows静态版: https://ffmpeg.zeranoe.com/builds/win64/static/下载nginx-rtmp-windows版:https://gi ...
- Dump 分析法
云更新目前能够收集32位客户机系统(XP和win7 32位)产生的蓝屏DMP文件到服务端DUMP文件夹,我们可以通过分析蓝屏曰志来确定到底是什么导致了客户机蓝屏. 一.WinDbg是什么?它能做什么? ...
- C++ 抽象类二(抽象类的基本语法)
//抽象类的基本语法 #include<iostream> using namespace std; /* 有关多继承的说明 被实际开发经验抛弃的多继承 工程开发中真正意义上的多继承是几乎 ...
- Container类是Component的子类,它也是一个抽象类,它允许其他的组件(Component)加入其中
在AWT中,所有能在屏幕上显示的组件(component )对应的类,均是 抽象类 Component 的子类或子孙类. 这些类均可继承Component类的变量和方法. Container类是Com ...
- 网页端的utf8和gb2312 之间关于osd 传参数的乱码问题
(0)HZK16 点阵字库原理及实现 (1)utf8 和 unicode gb2312之间的转换 (2)gb2312 的拓展 gbk 实现了更多的文字编码 像“瞭望塔”的瞭子在gb2312中是没有的 ...
- Sql Server数据批量更新
UPDATE S SET S.[name]=T.[name],S.[sch_id]=T.[sch_id],S.[sex]=T.[sex],S.[isOk]=T.[isOk] FROM [Student ...
- leetcode-happy number implemented in python
视频分析: http://v.youku.com/v_show/id_XMTMyODkyNDA0MA==.html?from=y1.7-1.2 class Solution(object): def ...
- Chem 3D模型的参数值更改方法
在化学绘图软件ChemOffice 15.1中有个专门用于绘制三维结构的组件,就是Chem 3D.通过这个组件用户可以绘制3D模型并可以通过这个组件来计算一些化学数据.在使用Chem 3D组件过程中, ...