本文主要是介绍【LeetCode】515. Find Largest Value in Each Tree Row【E】【87】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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]
Subscribe to see which companies asked this question.
广搜,对每层,直接记录最小的元素就行了
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = Noneclass Solution(object):def largestValues(self, root):if not root:return []res = [root.val]s = [root]while s:tval = - 1 << 32tnode = []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 sres += tval,return res[:-1]
这篇关于【LeetCode】515. Find Largest Value in Each Tree Row【E】【87】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!