Title Description
You need to find the maximum value in each row of the binary tree.
Thinking
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