Thirty-three face questions: subsequent binary search tree traversal sequence

 

An input array, a judgment is not a binary tree to find postorder result, if the returned array is not the same for any two numbers is true

Number binary search tree : if its left subtree is not empty, then the value of the left sub-tree, all the nodes are less than the value of the root node; if its right subtree is not empty, then all nodes in the right subtree point value greater than the value of its root node; its left and right sub-trees are binary sort tree.

After a clear definition of the binary tree search, a postorder traversal of the last value is the root node, the number of the root node before the two parts is used to indicate left and right subtrees

  //be=0;end=A.length

        boolean SquenceOfBST ( int A[], int be, int end ){       
            if(A==null ||a.length<=0) return false;
            int root=A[end-1];
           //左子树0->i-1
            int i=0;
            while(i<end-1){
                if(A[i]>root)
                    break;
                i++;
               }
           //右子树i->j-1
             int j=i;
             while(j<end-1){
                if(A[j]<root)
                    return false;
                j++;
              }
            boolean L=true;
            boolean R=true;
            if( i>0)    L=SquenceOfBST( A, be,i);
            if(i<end-1)  R=SquenceOfBST( A, i, end-i-1); //除去根节点
            return (A&&B);
        }

 

Guess you like

Origin www.cnblogs.com/niliuxiaocheng/p/12592345.html