题目描述
您需要在二叉树的每一行中找到最大的值。
思路
代码
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