public static TreeNode maxTree(int[] A) {
        // write your code here
        Stack<int[]>s1=new Stack<>();
        s1.push(new int[]{0,A.length,0});
        Stack<TreeNode>s2=new Stack<>();
        TreeNode root=null;
        while (!s1.isEmpty()) {
             int[]r=s1.pop();
             int s=r[0],e=r[1];
             int mi=s,mx=A[s];
             for (int i=s+1;i<e;++i) {
                 if (A[i]>mx) {
                     mi=i;
                     mx=A[i];
                 }
             }
             TreeNode node=new TreeNode(mx);
             if (s2.isEmpty()) {
                 s2.push(root=node);
             } else {
                 TreeNode p=s2.pop();
                 if (r[2]==0)
                    p.left=node;
                 else
                    p.right=node;
             }
             if (s!=mi) {
                 s2.push(node);
                 s1.push(new int[]{s,mi,0});
             }
             if (mi!=e-1) {
                 s2.push(node);
                 s1.push(new int[]{mi+1,e,1});
             }
        }
        return root;
    }