python stack method


  • 0
    class Solution(object):
        def largestValues(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            if root is None:
                return []
            stack = [root]
            r = []
            while len(stack) != 0:
                m = None
                temp = []
                while len(stack) != 0:
                    node = stack.pop()
                    if m is None:
                        m = node.val
                    else:
                        if m <node.val:
                            m = node.val
                    if node.left is not None:
                        temp.append(node.left)
                    if node.right is not None:
                        temp.append(node.right)
                stack = temp[:]
                r.append(m)
            return r

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.