LeetCode 515. find the maximum value in each tree row (BFS)

Title Description

You need to find the maximum value in each row of the binary tree.
Here Insert Picture Description

Thinking

See link

Code

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
Published 80 original articles · won praise 239 · Views 7065

Guess you like

Origin blog.csdn.net/weixin_37763870/article/details/104583825