LeetCode 515. 在每个树行中找最大值(广度优先搜索)

题目描述

您需要在二叉树的每一行中找到最大的值。
在这里插入图片描述

思路

详见链接

代码

class TreeNode:
	def __init__(self,x):
		self.val = x
		self.left = None
		self.right = None

class 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
发布了80 篇原创文章 · 获赞 239 · 访问量 7065

猜你喜欢

转载自blog.csdn.net/weixin_37763870/article/details/104583825
今日推荐