Python Iterative inorder solution


  • 0
    K
        def __init__(self):
            self.stack = []
            self.inorder_array = []
        def inOrder(self,root):
            current = root
            done = 0
            while not done:
                if current is not None:
                    self.stack.append(current)
                    current = current.left
                else:
                    if len(self.stack) > 0:
                        popped_item = self.stack[-1]
                        self.stack = self.stack[:-1]
                        self.inorder_array.append(popped_item)
                        current = popped_item.right
                    else:
                        done = 1
                        
        def isValidBST(self, root):
            self.inOrder(root)
            if all([self.inorder_array[i].val < self.inorder_array[i+1].val for i in xrange(len(self.inorder_array)-1)]):
                return True
            else:
                return False```

Log in to reply
 

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