问题描述

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的更多相关文章

  1. LeetCode赛题----Find Left Most Element

    问题描述 Given a binary tree, find the left most element in the last row of the tree. Example 1: Input: ...

  2. [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 ...

  3. 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 ...

  4. 【leetcode刷题笔记】Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  5. 【leetcode刷题笔记】Largest Rectangle in Histogram

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  6. 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 ...

  7. LeetCode赛题394----Decode String

    394. Decode String Given an encoded string, return it's decoded string. The encoding rule is: k[enco ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. C++类的构造函数及定义

    定义一个普通的类时,一定要定义它自己的构造函数.原因有三:第一个原因是编译器只有在发现类不包含任何构造函数的情况下才会替我们生成一个默认的构造函数,一旦我们定义了一些其他的构造函数,那么除非我们再定义 ...

  2. Mac 10.12原生方法对NTFS分区进行读写的配置

    说明:不一定有效,最简单的方法就是不用NTFS,直接FAT32,对于大文件就用切割. 方法: 1.确定U盘名称 diskutil list ls /Volumes/ 2.比如我找到的U盘名称为Unti ...

  3. java翻译到mono C#实现系列(4) 利用CountDownTimer类实现倒计时功能 mono版

    群里的朋友问利用CountDownTimer类实现倒计时功能怎么实现,我就百度了,参考http://blog.csdn.net/qq344429461/article/details/7521361写 ...

  4. (转)[小工具] Linux下列格式化工具 - column

    当你看到Linux下命令输出的列歪歪扭扭时,是不是看着很不爽?column 命令就可以方便地解决这个问题. 比如: 我们一般就只用到上面这一个用法. column的其他用法如下: 选项 含义 -c 字 ...

  5. LeetCode-2. Add Two Numbers(链表实现数字相加)

    1.题目描述 You are given two non-empty linked lists representing two non-negative integers. The digits a ...

  6. Java网络编程(一)

    Java网络编程: 1.1: 网络编程:对于我这个“研究不深”的网络菜鸟来说,我觉得网络编程就是实现计算机与计算机之间通信的编程.写些能够实现计算机与计算机之间的通信就行了(目前来说). 1.2:一台 ...

  7. redis数据类型(五)set类型

    一. set类型 set是无序集合,最大可以包含(2 的 32 次方-1)个元素. set 的是通过 hash table 实现的,所以添加,删除,查找的复杂度都是 O(1). hash table ...

  8. C++实现二叉排序树

    1.定义 二叉排序树(Binary Sort Tree),又称二叉查找树(Binary Search Tree),亦称二叉搜索树. 二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: (1)若左子 ...

  9. ios手势识别代理

    之前做优质派时写了个仿网易新闻导航的第三方,由于当时做项目时这个主控制器就是RootViewController,虽然用的是ScrollView但也没考虑到导航栏的手势返回的问题 ,现在做小区宝3.0 ...

  10. vim入门之配色方案(colorscheme)设置

    系统版本:ubuntu 16.04 LTS 刚开始用vim的时候,大家可能会觉得默认的语法高亮的颜色不合心意,不过对于vim来说,这并不是一个问题.其实vim的配色方案是可以更改的,既可以选择系统自带 ...