"""iterative solution"""


  • 0
    A

    class Solution:
    def largestValues(self, root):
    """
    :type root: TreeNode
    :rtype: List[int]
    """
    l=[]
    if(root==None):
    return l
    q1=[root]
    while(q1):
    q2=[]
    m=-9999999999
    while(len(q1)>0):
    a=q1.pop(0)
    if(a.val>m):
    m=a.val
    if(a.left):
    q2.append(a.left)
    if(a.right):
    q2.append(a.right)

            l.append(m)
            while(len(q2)>0):
                a=q2.pop(0)
                q1.append(a)
        return l

Log in to reply
 

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