[抄题]:

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. pyhdfs安装

    参考: http://blog.csdn.net/sinat_33741547/article/details/54428726 1.先更新pip,防止版本过低pip install --upgrad ...

  2. signapk

    signapk工具可以实现对安卓ROM和安卓应用进行签名.在安卓DIY与安卓ROM制作中作用是非常大的.可以使用其对经过自己DIY修改美化后的应用进行签名或对制作好的安卓ROM卡刷包进行签名.让我们做 ...

  3. <亲测>CentOS中yum安装ffmpeg

    CentOS中yum安装ffmpeg 1.升级系统 sudo yum install epel-release -y sudo yum update -y sudo shutdown -r now 2 ...

  4. element-ui的不稳定性

    伤脑筋的版本升级 element-ui升级到2.0版本了! element-ui作为比较成熟的广为人知的前端框架,原本满怀热情的去学习,也基于element-ui搭建出了一套系统,可是它居然升级了! ...

  5. pytorch下的lib库 源码阅读笔记(1)

    置顶:将pytorch clone到本地,查看initial commit,已经是麻雀虽小五脏俱全了,非常适合作为学习模板. 2017年12月7日01:24:15 2017-10-25 17:51 参 ...

  6. docker 小结

    1.docker修改镜像地址: /etc/docker/daemon.json 2.docker 启动容器: docker run -t -i ubuntu:14.04 /bin/bash 3.查看容 ...

  7. Python 爬虫 Vimeo视频下载链接

    python vimeo_d.py https://vimeo.com/228013581 在https://vimeo.com/上看到稀罕的视频 按照上面加上视频的观看地址运行即可获得视频下载链接 ...

  8. 3.Jmeter参数化

    1.参数化的三种方式 ${变量} 1 用户定义的变量 2 添加配置元件 CSV Data Set Config ,导入.csv文档 3 利用函数助手中的函数获取参数值     主要用 _Random函 ...

  9. tomcat advanced (RUNNING)

    1. 1. tomcat

  10. 61.纯 CSS 创作一只咖啡壶(这个不好看)

    原文地址:https://segmentfault.com/a/1190000015376202 感想: 好像不像呀,啊啊啊.伪元素.定位.动画.width和height包括内边距|边框|内容区. H ...