Iterative solution using queue (python 2.7)


#1

def solve(self, A):

    root=A
    q=[]
    q.append(root)
    q.append(None)
    arr=[]
    sum1=0
    maxy=0
    while(q):
        node=q.pop(0)
        if(node!=None):
            sum1+=node.val
        if(node==None):
            if(q!=[]):
                q.append(None)
                sum1=0
                
        else:        
            if(node.left and node!=None):
                q.append(node.left)
            if(node.right and node!=None):
                q.append(node.right)
                
        maxy=max(sum1,maxy)
            
    return(maxy)