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   9 

Output: [1, 3, 9]

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

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

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

[一句话思路]:

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

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

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

[总结]:

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

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

[关键模板化代码]:

[其他解法]:

[Follow Up]:

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

[代码风格] :

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

[潜台词] :

原文地址:https://www.cnblogs.com/immiao0319/p/9379977.html

时间: 2024-10-08 16:11:12

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

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 9 Output: [1, 3, 9] 题意:找出二叉树一层的最大值 解法:用BFS的方式遍历每一层 class Solution(object): def largestValues(self, root): """ :type root: TreeNode :rtype: Li

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

在二叉树的每一行中找到最大的值.示例:输入:           1         /  \        3   2       /  \    \        5   3    9 输出: [1, 3, 9] 详见:https://leetcode.com/problems/find-largest-value-in-each-tree-row/description/ C++: /** * Definition for a binary tree node. * struct Tree

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 9 Output: [1, 3, 9] 经典bfs, 不多说 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeN

(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 9 Output: [1, 3, 9] --------------------------------------------------------------------------就是找出二叉树的每一层的最大数.用BFS较为简单. C++代码: /** * Definition for a b

[LeetCode]515 Find Largest Value in Each Tree Row(dfs)

题目链接:https://leetcode.com/problems/find-largest-value-in-each-tree-row/?tab=Description 题意:找每层的最大值. 直接dfs扔map里 1 class Solution { 2 public: 3 int sz; 4 map<int, int> ret; 5 void dfs(TreeNode* p, int d) { 6 sz = max(sz, d); 7 if(ret.find(d) == ret.en

[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 Output: [1, 3, 9] 这道题让我们找二叉树每行的最大的结点值,那么实际上最直接的方法就是用层序遍历,然后在每一层中找到最大值,加入结果res中即可,参见代码如下: 解法一: class Solution { public: vector<int> largestValues(Tre

[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 9 Output: [1, 3, 9] 思路: 层次遍历,每一层选出当前层最大值. vector<int> largestValues(TreeNode* root) { vector<int>result; if (root == NULL)return result;

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 Output: [1, 3, 9] ''' # Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self

LeetCode: Find Largest Value in Each Tree Row

BFS 1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 public List<Integer> largestValues(TreeNode ro