Clean Python solution


  • 0
    G

    '''

    def inorderTraversal(self, root):
        if not root: return []
        qstack=[root]
        visit=set()
        res=[]
        while qstack:
            node=qstack[-1]
            if node.left and node.left not in visit:
                qstack.append(node.left)
            else:
                res.append(node.val)
                visit.add(node)
                qstack.pop()
                if node.right:
                    qstack.append(node.right)
        return res
    

    '''

    Or

    '''

    def inorderTraversal(self, root):
        if not root: return []
        qstack=[root]
        last=None
        res=[]
        while qstack:
            node=qstack[-1]
            if node.left and (last==None or node==last.left or node==last.right):
                qstack.append(node.left)
            else:
                res.append(node.val)
                qstack.pop()
                if node.right:
                    qstack.append(node.right)
            last=node
        return res
    

    '''


Log in to reply
 

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