Solution for Preorder Traversal iteratively


  • 0
    J

    My solution to preorder traversal using iterative method
    '''

    def preorderTraversal(self, root):
        stack = []
        ret = []
        stack.append(root)
        
        if (root == None):
            return ret
        while(len(stack)>0):
            node = stack.pop()
            ret.append(node.val)
            if(node.right != None):
                stack.append(node.right)
            if(node.left != None):
                stack.append(node.left)
        return ret
    

    '''


Log in to reply
 

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