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 ...
随机推荐
- 社区发现SLPA算法
社区(community)定义:同一社区内的节点与节点之间关系紧密,而社区与社区之间的关系稀疏. 设图G=G(V,E),所谓社区发现是指在图G中确定nc(>=1)个社区C={C1,C2,..., ...
- 编程开发之--java多线程学习总结(5)
4.对继承自Runnable的线程进行锁机制的使用 package com.lfy.ThreadsSynchronize; import java.util.concurrent.locks.Lock ...
- [转] Scala Async 库 (Scala future, await, async)
[From] https://colobu.com/2016/02/15/Scala-Async/ 在我以前的文章中,我介绍了Scala Future and Promise.Future代表一个异步 ...
- ContentProvider和Cursor以及CursorAdapter三者之间内部链接实现原理 解析
最近 在学习Android3.0中推出的 Loader 机制,其中CursorLoader 这个加载器说是可以实时监测数据和更新数据,为了一探究竟,就连带的将 ContentProvider和Curs ...
- java.lang.Exception: The server rejected the connection: None of the protocols were accepted
solution for this is from comment for https://issues.jenkins-ci.org/browse/JENKINS-29616. The follow ...
- MySQL Migration Tool报“initialized java loader”错误的问题
MySQL Migration Tool报“initialized java loader”错误的问题 运行MySQL Migration Tool时经常会提示“An error occured ...
- windows系统PHP7开启curl_init
1.php.ini,开启extension=php_curl.dll,去掉去掉前面的“;” 2.检查php.ini的extension_dir值是哪个目录(也就是插件扩展目录,比如php_curl.d ...
- MYSQL 缓存
在PHP.INI中query_cache_type设置为1. 即 开始MYSQL全局SQL语句 都缓存.(0 不使用) 临时关闭查询缓冲的方法:1. SELECT SQL_NO_CACHE fi ...
- [中英对照]Booting Process in Linux RHEL 7 | Linux RHEL 7启动过程
Booting Process in Linux RHEL 7 | Linux RHEL 7启动过程 In this post, I will guide you booting process in ...
- STL中deque 解析
一.deque的中控器 deque是连续空间(至少逻辑上看来如此),连续线性空间总令我们联想到array或vector.array无法成长,vector虽可成长,却只能向尾端成长,而且其所谓的成长原是 ...