[Prove safety the offer] subsequent binary search tree traversal sequence (as judged by an array of subsequent traversal it is not BST)

① title

    Enter an integer array, the array is not the result of the determination after traversing a binary search tree. If the output Yes, otherwise the output No. Suppose the input array of any two numbers are different from each other.

② ideas

    1, in a subsequent traversal of the array, the last element is the root.

    2, the BST, the left subtree of each element <root <right subtree of each element

    3, bit 0 from the beginning, first find> root element, this recording position i. Prior to this position belongs to the left sub-tree (in this case have concluded that less than the left subtree root node) 

    4, check if greater than the right subtree with nodes (starting from the i-th bit before the root node) 

    5, left and right subtrees recursively determined whether the BST belongs, i.e., repeat steps 3-4;

③ Code

  

. 1  public  class Solution {
 2      public  Boolean VerifySquenceOfBST ( int [] Sequence) {
 . 3          Boolean [] RES = new new  Boolean [. 1 ];
 . 4          RES [0] = to true ;
 . 5          IF (sequence.length == 0)          // if the input is empty, then directly back to false 
. 6              return  to false ;
 . 7          isBST (0,. 1-sequence.length, RES, Sequence);   // call the function, the function can be altered, where res [0] is determined according to various conditions 
. 8          return RES [ 0 ];
 9      }
 10     
. 11      public  int isBST ( int Start, int End, Boolean [] RES, int [] Sequence) {
 12 is              IF (Start> End =)    // . 1 Species exit condition 
13 is                  return Start;
 14              int MID = (Start + End)> >> 1;     // divided by 2 
15              int curr_root = Sequence [End];
 16              int i = Start;    // this step can not be written in the for loop, otherwise i would not recognize an error in line 21 
17              for (; I <End; I ++ ) {
 18 is                  IF (Sequence [I]> curr_root)
 . 19                      BREAK;    // once the first> root element found immediately break out 
20 is              }
 21 is              for ( int J = I; J <End; J ++ ) {
 22 is                  IF (Sequence [J] < curr_root) { 
 23 is                      RES [0] = false ;    // change res [0] the value of 
24-                      return Start;    // isBST this function just to change res [0] value, so just return a something on the line, for example, I return a Start 
25                  }
 26              }
 27              isBST (Start,. 1-MID, RES, Sequence);   // recursive Analyzing left subtree, 
28              isBST (MID,-End. 1, RES, Sequence);   //Recursively determining the right subtree 
29              return Start;
 30      }
 31 is }

④ learned

    1, do 108 questions Leeccode, so write to the Law >>> used in the line 14 questions.

    2, we must learn how to iterate order after a judgment is not BST, is the analysis of ② point of this question;

    3, using the res [0] flag, and with the entire isBST function to change the res [0] value that isBST this function's return value to return what is not important, this approach me from the "Programmer Code Interview Guide: IT corporate name title algorithms and data structures optimal solution, "the 145 learned.

Guess you like

Origin www.cnblogs.com/zf007/p/11668835.html