[抄题]:

You need to find the largest value in each row of a binary tree.

Example:

Input: 

          1
/ \
3 2
/ \ \
5 3 9 Output: [1, 3, 9]

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么确定每一行的大小:不熟悉bfs。其中q每次只存了一行,所以size就是当前数组的大小

[英文数据结构或算法,为什么不用别的数据结构或算法]:

Queue<TreeNode> q = new LinkedList<>(); 因为都可以随便动?

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

BFS要点:(3先生)先加头、先判Empty、先取长度

[复杂度]:Time complexity: O(n) Space complexity: O(n)

图是v+e 树就是点数n

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

// package whatever; // don't place package name!

import java.io.*;
import java.util.*;
import java.lang.*; class TreeNode
{
int val;
TreeNode left, right; //parameter is another item
TreeNode(int item) {
val = item;
left = right = null;
}
} class Solution {
TreeNode root; public List<Integer> largestValues(TreeNode root) {
//ini: q, int max, Array
int max = Integer.MIN_VALUE;
//implement by linkedlist
Queue<TreeNode> q = new LinkedList<>();
List<Integer> answer = new ArrayList<Integer>(); //cc
if (root == null) return answer; q.offer(root);
while (!q.isEmpty()) {
//
int size = q.size(); for (int i = 0; i < size; i++) {
TreeNode cur = q.poll();
max = Math.max(cur.val, max);
if (cur.left != null) q.offer(cur.left);
if (cur.right != null) q.offer(cur.right);
}
//add to answer
answer.add(max);
//renew max
max = Integer.MIN_VALUE;
} return answer;
}
} class MyCode {
public static void main (String[] args) {
Solution tree = new Solution();
tree.root = new TreeNode(1);
tree.root.left = new TreeNode(2);
tree.root.right = new TreeNode(3);
tree.root.left.left = new TreeNode(4);
tree.root.left.right = new TreeNode(5); //TreeNode t = tree.upsideDownBinaryTree(tree.root);
List<Integer> answer = tree.largestValues(tree.root);
int size = answer.size();
for (int i = 0; i < size; i++)
System.out.println("answer[i] = " + answer.get(i));
}
}

[潜台词] :

515. Find Largest Value in Each Tree Row查找一行中的最大值的更多相关文章

  1. LN : leetcode 515 Find Largest Value in Each Tree Row

    lc 515 Find Largest Value in Each Tree Row 515 Find Largest Value in Each Tree Row You need to find ...

  2. 515 Find Largest Value in Each Tree Row 在每个树行中找最大值

    在二叉树的每一行中找到最大的值.示例:输入:           1         /  \        3   2       /  \    \        5   3    9 输出: [ ...

  3. (BFS 二叉树) leetcode 515. Find Largest Value in Each Tree Row

    You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...

  4. 【LeetCode】515. Find Largest Value in Each Tree Row 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...

  5. 515. Find Largest Value in Each Tree Row

    You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...

  6. LeetCode 515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)

    515. 在每个树行中找最大值 515. Find Largest Value in Each Tree Row 题目描述 You need to find the largest value in ...

  7. Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)

    Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...

  8. [LeetCode] Find Largest Value in Each Tree Row 找树每行最大的结点值

    You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...

  9. [Swift]LeetCode515. 在每个树行中找最大值 | Find Largest Value in Each Tree Row

    You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...

随机推荐

  1. JS笔记汇总

      注释必须要多写! 1.方便后台看 2.方便自己查错和优化 事先先沟通约定好,比如交互的数据格式需求是怎么样的啊,功能模块的逻辑是怎么样的等等.提前先和产品还有后台沟通好.   JSON内不能包含注 ...

  2. Spring IOC(四)总结升华篇

    本系列目录 Spring IOC(一)概览 Spring IOC(二)容器初始化 Spring IOC(三)依赖注入 Spring IOC(四)总结升华 =============正文分割线===== ...

  3. py-day3-3 python 函数的作用域

    def test1(): print('in the test1') def test(): print('in the test') return test1 print(test) res = t ...

  4. JavaScript中的匿名函数遇上!会怎么样

    通常,我们声明一个函数test){},可以通过test()来调用这个函数.但是,如果我们在这个函数声明的末尾加上(),解析器是无法理解的. function test(){ console.log(' ...

  5. ThinkPHP5.*版本发布安全更新

    2018 年 12 月 9 日 发布 本次版本更新主要涉及一个安全更新,由于框架对控制器名没有进行足够的检测会导致在没有开启强制路由的情况下可能的getshell漏洞,受影响的版本包括5.0和5.1版 ...

  6. java.lang.Integer源码浅析

    Integer定义,final不可修改的类 public final class Integer extends Number implements Comparable<Integer> ...

  7. java 性能测试框架工具-junitperf

    性能测试工具 对于 Java 开发者来说,要去学习性能测试工具未免很麻烦. 但有时候会有性能测试的需求. junitperf junitperf 就是一款为 Java 开发者设计的性能测试框架,如果你 ...

  8. python中class的序列化和反序列化

    对于类的序列化:将类的成员变量名和数据作为一对键值对存储在物理内存中,例如 class A(object): def __init__(self): self.a = o self.b = 1 sel ...

  9. 工具函数(获取url , 时间格式化,随机数)

    (function(window,$){ function Tools() { } // url Tools.prototype.readUrlToParams = function() { var ...

  10. LINUX 中实现逻辑卷、自动挂载

    实验项目: 准备3块10G的空闲分区,将类型ID修改为8e(LVM) 使用其中2块分区组建名为myvg的卷组,查看此卷组信息 先检查有哪些物理卷 讲两块空闲分区转换成物理卷 再检查有哪些物理卷,查看其 ...