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.right = None

这道题描述的是:  给我们一颗二叉树的根节点,返回每一层最大值的数组

思想:  广度优先遍历,把每一层最大值放入结果列表

python实现:
 1 class Solution(object):
 2     def largestValues(self, root):
 3         """
 4         :type root: TreeNode
 5         :rtype: List[int]
 6         """
 7         if not root:
 8             return []
 9         levels = [[root]]
10         values = [[root.val]]
11         for level in levels:
12             next_level = []
13             value = []
14             for node in level:
15                 if node.left is not None:
16                     next_level.append(node.left)
17                     value.append(node.left.val)
18                 if node.right is not None:
19                     next_level.append(node.right)
20                     value.append(node.right.val)
21             if next_level:
22                 levels.append(next_level)
23                 values.append(value)
24         res = []
25         for v in values:
26             res.append(max(v))
27         return res
精简一下代码:
 1 class Solution(object):
 2     def largestValues(self, root):
 3         """
 4         :type root: TreeNode
 5         :rtype: List[int]
 6         """
 7         values = []
 8         level = [root]
 9         while any(level):
10             values.append( max( [i.val for i in level] ) )
11             level = [child for node in level for child in [node.left, node.right ] if child is not None]
12         return values
				
时间: 2024-10-08 16:11:15

leetcode算法: Find Largest Value in Each Tree Row的相关文章

(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算法题-Convert BST to Greater Tree(Java实现)

这是悦乐书的第255次更新,第268篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第122题(顺位题号是538).给定二进制搜索树(BST),将其转换为更大树,使原始BST的每个键都更改为原始键加上所有键的总和大于BST中的原始键.例如: 输入:二进制搜索树的根,如下所示: 5 / 2 13 输出:大树的根,如下所示: 18 / 20 13 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试. 0

[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: 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

leetcode算法:Trim a Binar Search Tree

Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary

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就是当前数组的大小 [英文

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

[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;