Python 3 with explanation


#1

class Solution:
# @param A : list of integers
# @return an integer
def solve(self, A):
In pre-order traversal we can visualize that if we encounter greater number than
root node (or current node) then all number after that encountered greater number
should be greater than root node(or current node),if it happens then return (1)
otherwise return (0)

    root=float("-infinity")
    stack=[]
    for i in range(len(A)):
        if(A[i]<root):
            return(0)
        if(stack and stack[-1]<A[i]):
            root=stack.pop()
        stack.append(A[i])
    return(1)

#2

Bro, I think you should keep popping WHILE stack is not empty and stack[-1]<A[i].