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 ...
随机推荐
- redis命令使用
set key value get key 删除key (返回被移除key的数量.):del key 检查给定key是否存在(若key存在,返回1,否则返回0.):exists key > ex ...
- Jmeter负载测试例子
通过浏览器操作网站在jmeter录屏控制器显示录屏例子,并且通过这例子模拟多用户(线程)来负载测试. 工具/原料 Jmeter 浏览器 1.先在测试计划创建线程组和录制Case 1 1.1 选 ...
- ubuntu 终端$换行
编辑~/.bashrc文件 sudo gedit ~/.bashrc 红色部分为添加的 \n if [ "$color_prompt" = yes ]; then PS1= ...
- vi 的使用,很详细
来源:鸟哥的Linux私房菜 网址:http://vbird.dic.ksu.edu.tw/linux_basic/0310vi_2.php
- 如何在ChemDraw 15.1 Pro中添加模板
ChemDraw化学绘图工具为了方便用户的使用,特地开发了众多的各种类型模板.用户在绘制一些简单或复杂的化学结构式时,可以直接从ChemDraw模板库里直接调用使用,虽然ChemDraw模板非常的丰富 ...
- git base 简单命令行
记录几个简单的命令 1:克隆-把线上的文件复制到本地 git clone 线上地址 2:检查状态 git status 3:放入待仓储 git add.文件名 git add * (全部文件,简单粗暴 ...
- 《JAVA多线程编程核心技术》 笔记:第七章:拾遗增补
一.线程的状态 1.1 状态种类及理解:(一共6个) 文字说明和理解: NEW状态:线程实例化后还从未执行start()方法时的状态: RUNNABLE状态:线程进入运行的状态: TERMINATED ...
- MySQL 1067
今天在云服务器上装mysql的时候,启动突然报了一个“1067 进程意外终止”的错误,这个错误之前是遇到过的,之前因为my.ini配置basedir路径的时候没有正确配置导致了这个错误,但是今天又出现 ...
- vbs 修改Administrator帐号密码
Dim WshShell, oExec Set wshShell = CreateObject("WScript.Shell") Set objFSO = CreateObject ...
- 数据库操作(使用FMDB)
iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.PlausibleDatabase.sqlitepers ...