Python Solution Ami


#1
def preorderTraversal(self, A):
        tot=[]
        parents=[]
        x=A        
        while(x):
            if(x.val!=0.5):
                tot+=[x.val]
                x.val=0.5
            if(x.left and x.left.val!=0.5):
                parents+=[x]
                x=x.left
            elif(x.right and x.right.val!=0.5):
                parents+=[x]
                x=x.right
            else:
                if(len(parents)==0):
                    break
                if(parents[-1].left and parents[-1].left.val==0.5):
                    parents[-1].left=None
                if(parents[-1].right and parents[-1].right.val==0.5):
                    parents[-1].right=None
                x=parents[-1]
                parents.pop()
        return tot