[抄题]:

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. PythonStudy——集合 Set

    # 空集合:不能用{},因为用来标示空字典 s = set() print(s, type(s)) # 概念:# 1.set为可变类型 - 可增可删# 2.set为去重存储 - set中不能存放重复数 ...

  2. cmake编译obs

    https://blog.csdn.net/su_vast/article/details/74984213 https://blog.csdn.net/u011258240/article/deta ...

  3. Ubuntu 下解压tar.xz方法

    参考地址:https://www.cnblogs.com/baby123/p/6611169.html

  4. python学习路程1

    常用的转义字符还有: \n 表示换行 \t 表示一个制表符 \\ 表示 \ 字符本身 任务 请将下面两行内容用Python的字符串表示并打印出来: Python was started in 1989 ...

  5. angularjs 的模型无法绑定到隐藏域(input hidden)

    描述一下问题: 在操作表单中的隐藏域的时候发现angularjs的模型无法绑定,比如: <input type="hidden" name="someData&qu ...

  6. MVC Ajax.BeginForm 提交上传图片

    吃水不忘挖井人,如果对你有帮助,请说声谢谢.如果你要转载,请注明出处.谢谢! 异步提交时,出现图片不能上传. 起初我定格在  System.Web.Mvc  中.查询源码时,也是没有问题的.那问题出现 ...

  7. Delphi在调WebService的时候加Soap头验证

    procedure   ws: WebServiceSoap;   H: XXXHeader; begin   ws := GetWebServiceSoap;   H := XXXHeader.Cr ...

  8. centos6.5虚拟机无法访问外网解决办法

    安装了centos6.5虚拟机,使用的是桥接方式.把所有的配置已经写到/etc/sysconfig/network-scripts/ifcfg-eth0中后,发现内网可以ping通,外网却无法访问. ...

  9. Calling Custom Actions from JavaScript

    原文地址:https://www.wipfli.com/insights/blogs/connect-microsoft-dynamics-365-blog/160810-calling-custom ...

  10. 如何使用jQuery从字符串中删除最后一个字符

    如何使用jQuery从字符串中删除最后一个字符 1.string.slice(0,-1) 2.str.substring(0,str.length-1)