问题描述

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. (转)Linux下同步工具inotify+rsync使用详解

    原文:https://segmentfault.com/a/1190000002427568 1. rsync 1.1 什么是rsync rsync是一个远程数据同步工具,可通过LAN/WAN快速同步 ...

  2. 拖进Xshell终端窗口文件上传

    XShell已经内置rz 直接从Windows拖文件进去终端 http://www.jb51.net/LINUXjishu/163820.html 借助securtCRT,使用linux命令sz可以很 ...

  3. Fedora 24 python3.5 安装M2Crypto

    安装M2Crypto#python3 -m pip install M2Crypto 出现错误 gcc: /usr/lib/rpm/redhat/redhat-hardened-cc1:Nosuch ...

  4. UTF8最好不要带BOM,附许多经典评论

    UTF-8 不需要 BOM,尽管 Unicode 标准允许在 UTF-8 中使用 BOM.所以不含 BOM 的 UTF-8 才是标准形式,在 UTF-8 文件中放置 BOM 主要是微软的习惯(顺便提一 ...

  5. I/O的整体介绍

    java的i/o操作类在包java.io下,大概可以分成如下四组: 基于字节操作的 I/O 接口:InputStream 和 OutputStream 基于字符操作的 I/O 接口:Writer 和 ...

  6. Java的枚举类型

    引用并转载于:http://blog.csdn.net/ishallwin/article/details/9440251 1.什么是枚举: 在实际编程中,往往存在着这样的“数据集”,它们的数值在程序 ...

  7. Django url分发到工程里

    因为我们建立了Django后 ,url是在mysite下的全局对象 因为我们实际项目里不可能只有一个工程 而全放在全局里去分发url 会让代码耦合度提高,代码量大后会造成维护困难.这时候我们把url分 ...

  8. 在ViewDidLoad中往导航栈推ViewController报错

     Unbalanced calls to begin/end appearance transitions for <YZPMainViewController: 0x7fa04b4970f0& ...

  9. Angular: 使用 RxJS Observables 来实现简易版的无限滚动加载指令

    我使用 angular-cli 来搭建项目. ng new infinite-scroller-poc --style=scss 项目生成好后,进入 infinite-scroller-poc 目录下 ...

  10. iOS开源项目周报0316

    由OpenDigg 出品的iOS开源项目周报第十二期来啦.我们的iOS开源周报集合了OpenDigg一周来新收录的优质的iOS开源项目,方便iOS开发人员便捷的找到自己需要的项目工具等.GodEye  ...