You need to find the largest value in each row of a binary tree.
Example:
Input: 1 / \ 3 2 / \ \ 5 3 9 Output: [1, 3, 9]
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def largestValues(self, root):
if not root:
return []
res = [root.val]
s = [root]
while s:
tval = - 1 << 32
tnode = []
for i in s:
if i.left != None:
tnode += i.left,
tval = max(tval,i.left.val)
if i.right != None:
tnode += i.right,
tval = max(tval,i.right.val)
s = tnode[:]
#print s
res += tval,
return res[:-1]