32ms Simple Python Solution


  • 0
    J

    snippet not so pretty.

    import collections
    
    def inorderTraversal(self, root):
        result = collections.deque()
        stack = collections.deque()
        
        node = root
        while True:
            if node is None:
                if not stack:
                    break
                
                node, val = stack.pop()
                result.append(val)
                
                continue
                    
            if node.left is None:
                result.append(node.val)
                node = node.right
            else:
                stack.append((node.right, node.val))
                node = node.left
        
        return list(result)
    

Log in to reply
 

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