题目描述
您需要在二叉树的每一行中找到最大的值。
思路
详见链接
代码
class TreeNode:def __init__(self,x):self.val = xself.left = Noneself.right = Noneclass Solution:def largestValues(self,root:TreeNode) -> List[int]:res = []def helper(root,depth):if not root:return if len(res) == depth:res.append([])res[depth].append(root.val)helper(root.left,depth+1)helper(root.right,depth+1)helper(root,0)ans = []for i in res:ans.append(max[i])return res
如果觉得《LeetCode 515. 在每个树行中找最大值(广度优先搜索)》对你有帮助,请点赞、收藏,并留下你的观点哦!