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 ...
随机推荐
- git 入门一(初识)
分布式版本控制系统 & 集中式版本控制系统 分布式版本控制系统( Distributed Version Control System)在这类系统中,像 Git,Mercurial,Baz ...
- 2017-2018 ACM-ICPC, NEERC, Northern Subregional Contest D Dividing Marbles
题目大意: 给出一个$N(N <= 2^{22}$),$N$的二进制表示中1的个数不超过4. 一开始有一个集合$S = {N}$, 每次操作可以选择$n\in S \ (n > 1)$, ...
- 一个事半功倍的c#方法 动态注册按钮事件
前几天在网上看见一个制作计算器的c#程序,其中有一个动态注册按钮事件,觉的很有用.于是实际操作了一哈, 确实比较好. 言归正传,下面就来讲讲怎样动态注册按钮事件. 首先,我们需要设置变量来获取点击一个 ...
- ZeroMQ一个更小、更快、更简单的智能传输层协议
这个githube上的教程是非常好的,是个中文翻译,大家直接学这个就行 https://github.com/anjuke/zguide-cn/tree/master/bin 原文地址: https: ...
- 组合使用QT的资源管理高级功能简化开发过程
使用 QT 进行团队开发的时候,常常碰到一个问题,就是如何共同管理资源?甚至一个人进行开发的时候如何简化资源的维护,避免无谓的消耗? 如果可以做到在开发的时候,大家把美工做的图片(往往是程序员先自己随 ...
- DataTable To Entity
using System;using System.Collections.Generic;using System.Data;using System.Reflection;using System ...
- iOS 如何用JSONKit读写JSON文件
如何用JSONKit读写JSON文件 分类: ios2013-04-20 12:46 510人阅读 评论(0) 收藏 举报 JSON文件格式简单,使用方便,值得一用. 目前已经有多个库支持Json文 ...
- 大话Python程序的命名规范
1.全局变量名: 全部大写 MY_GLOBAL_VAR 2. 类名: 首字母大写,总是使用首字母大写单词串,如MyClass,内部类可以使用额外的前导下划线: 3.普通变量,普通函数名,文件名: 全部 ...
- Powershell About Active Directory Group Membership of a domain user
使用Get-User命令去寻找group membership of a domain user $((Get-ADUser Wendy -Properties *).MemberOf -split ...
- 安装 oracle 11g 发行版 出现错误:由于以下错误,Enterprise Manager配置失败,启动Database Control时出错
解决方案如下进入dos(1)查看dbconsole状态:emctl status dbconsole 显示:Environment variable ORACLE_SID not defined. P ...