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 ...
随机推荐
- Flea Circus(Project Euler 213)
original version hackerrank programming version 题目大意是N*N的格子,每个格子一开始有1个跳蚤,每过单位时间跳蚤会等概率向四周跳,问M秒后空格子的期望 ...
- html input accept类型
在上传文件的时候,需要限制指定的文件类型. <input type="file" accept="image/*" /> accept表示可以上传文 ...
- asp.net页面触发事件panel滚动条高度不变的实现方法
asp.net页面按钮点击触发事件后panel滚动条非自动回到顶端,每次都要往下拉一下,关于这个问题的解决方法如下 此文是为解决asp.net页面按钮点击触发事件后panel滚动条非自动回到顶端的解决 ...
- ConfigurationSection类使用心得
ConfigurationSection类主要是方便我们用于扩展自定义webcongfig中的节点信息.我们可以方便的通过以下方式获取[自定义节点对象] [你自定义的对象] config = ([你自 ...
- Array转为Json需要导入的包
今天自己写了一个JSON的例子,可以一调用就出了问题,报下面这个异常: java.lang.ClassNotFoundException: org.apache.commons.lang.except ...
- 【BZOJ2208】[Jsoi2010]连通数 DFS
[BZOJ2208][Jsoi2010]连通数 Description Input 输入数据第一行是图顶点的数量,一个正整数N. 接下来N行,每行N个字符.第i行第j列的1表示顶点i到j有边,0则表示 ...
- 160815、mysql主从复制/读写分离
mysql主从复制主服务器IP:192.168.99.10从服务器IP:192.168.99.20(一)安装mysql(主从服务器操作相同)yum -y install gcc gcc-c++ ncu ...
- Spring MVC 多语言化的实践和学习
一.主要参考: SpringMVC简单实现国际化/多语言 - CSDN博客 https://blog.csdn.net/u013360850/article/details/70860144/ 二.总 ...
- windows程序查看可以行文件依赖库
通常在做windows下开发程序,发布的时候需要同时打包一些依赖库:我们可以通过工具直接查看需要发布的程序依赖的程序,这样可以方便快捷的打包程序 这里我们推荐使用:dependencywalker 下 ...
- SQL server-自增主键
1.CREATE TABLE 表名( 字段名 [int] IDENTITY (1, 1) NOT NULL , //--(seed = 1,incre ...