LeetCode赛题515----Find Largest Element in Each Row
问题描述
You need to find the largest element in each row of a Binary Tree.
Example:
Input:
1
/ \
2 3
/ \ \
5 3 9
Output: [1, 3, 9]
算法分析
使用两个队列,逐层遍历二叉树的各个节点,每个队列中的节点都是同一层的节点,在遍历一层时,找出该层最大的节点值加入ArrayList中。
Java算法实现:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int[] findValueMostElement(TreeNode root) {
if(root==null){
int[] tmp=new int[0];
return tmp;
}
ArrayList<Integer>arr=new ArrayList<>();
Queue<TreeNode>que1=new LinkedList<>();
Queue<TreeNode>que2=new LinkedList<>();
que1.add(root);
int max;
while(!que1.isEmpty()||!que2.isEmpty()){
max=Integer.MIN_VALUE;
if(!que1.isEmpty()){
while(!que1.isEmpty()){
TreeNode node=que1.poll();
if(node.val>max){ //找到该层的最大值
max=node.val;
}
if(node.left!=null){
que2.add(node.left);
}
if(node.right!=null){
que2.add(node.right);
}
}
arr.add(max);
}
else if(!que2.isEmpty()){
while(!que2.isEmpty()){
TreeNode node=que2.poll();
if(node.val>max){
max=node.val;
}
if(node.left!=null){
que1.add(node.left);
}
if(node.right!=null){
que1.add(node.right);
}
}
arr.add(max);
}
}
int [] ans=new int[arr.size()];
for(int i=0;i<ans.length;i++){
ans[i]=arr.get(i);
}
return ans;
}
}
LeetCode赛题515----Find Largest Element in Each Row的更多相关文章
- LeetCode赛题----Find Left Most Element
问题描述 Given a binary tree, find the left most element in the last row of the tree. Example 1: Input: ...
- [LeetCode&Python] Problem 703. Kth Largest Element in a Stream
Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...
- LeetCode(215) Kth Largest Element in an Array
题目 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the ...
- 【leetcode刷题笔记】Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【leetcode刷题笔记】Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- LeetCode赛题395----Longest Substring with At Least K Repeating Characters
395. Longest Substring with At least K Repeating Characters Find the length of the longest substring ...
- LeetCode赛题394----Decode String
394. Decode String Given an encoded string, return it's decoded string. The encoding rule is: k[enco ...
- LeetCode赛题393----UTF-8 Validation
393. UTF-8 Validation A character in UTF8 can be from 1 to 4 bytes long, subjected to the following ...
- LeetCode赛题392---- Is Subsequence
392. Is Subsequence Given a string s and a string t, check if s is subsequence of t. You may assume ...
随机推荐
- 常见的http错误提示
1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态代码. 代码 说明100 (继续) 请求者应当继续提出请求.服务器返回此代码表示已收到请求的第一部分,正在等待其余部分. 101 (切换协议 ...
- spark持久化
spark持久化:cache .persist.checkpoint 一.cache持久化 cache实际上是persist的一种简化方式,是一种懒执行的,执行action类算子才会触发,cahce后 ...
- python学习,day3:函数式编程,带参数
# coding=utf-8 # Author: RyAn Bi def test(x,y,z): print(x) print(y) print(z) test(y=2,z =3,x=1) #形参与 ...
- python全栈开发_day13_迭代器和生成器
一:迭代器 1)可迭代对象 具有内置函数__iter__的数据就是可迭代对象 2)迭代器对象 具有内置函数__next__的数据就是迭代器对象 迭代器对象一定是可迭代对象,可迭代对象不一定是迭代器对象 ...
- js 实现星级评分
最近的项目中有一个星级评分的需求, 自己就写了一下, 由于可能一个页面要用到多个,就采用了面向对象的写法. 用到的png图片也放到这里. js要用到jquery. css: .sr-star{ ...
- Openerp 添加修改报表
Report Designer 模块在生成新报表的时候是有BUG的不建议直接使用,不过我们也可以通过该插件再写简单的代码来实现新添加报表,插件安装成功后我们可以按照下列方法来添加报表 OpenERP ...
- 【es6】数组扩展
只有一个参数,为数组中的值.
- Pycharm的配置和使用
pycharm pycharm是一个比较好的python IDE,可以在MACOS和windows上使用,补全功能强大,而且界面十分友好,特别适合python编程人员使用. pycharm Pycha ...
- docker最新版本如何自定义配置文件
1 如果你想使用 /etc/default/docker文件配置你的docker 在 /etc/systemd/system/docker.service.d/docker.conf 添加下面---- ...
- 安装Postgresql之后,创建用户 配置rails
登录 sudo su - postgres psql 1 创建Postgresql新用户,devpg是用户名,密码也是devpg, 不是超级管理员,拥有创建数据库权限,登录权限,继承拥有角色权限 cr ...