515. 在每个树行中找最大值



BFS模板的应用。

总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html

class Solution(object):
    def largestValues(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root:
            return []
        ans = []
        stack = [root]
        while stack:
            temp = []
            sizeStack = len(stack)
            for i in range(sizeStack):
                node = stack.pop(0)
                temp.append(node.val)
                if node.left:
                    stack.append(node.left)
                if node.right:
                    stack.append(node.right)
            ans.append(max(temp))
        return ans
原文地址:https://www.cnblogs.com/panweiwei/p/13661673.html