Title: Enter an array of integers to determine if the array is the result of a sequential traversal of a binary search tree. Returns true if yes, otherwise false. Assume that any two digits of the input array are different. For example, the input array {5,7,6,9,10,8} returns true because the integer sequence is the result of a post-order traversal of a two-fork search tree. If the input array is {7,4,6,5}, it returns false because there is no order for the binary search tree to be sequenced. In the sequence of sequential traversal, the last number is the value of the root node of the tree. The preceding number in the array can be divided into two parts: the first part is the value of the left subtree node, they are smaller than the root node, and the second part is the value of the right subtree node, which is larger than the value of the root node. Take the array {5,7,6,9,11,10,8} as an example, the last number 8 of the post-order traversal result is the value of the root node. In this array, the first 3 numbers 5,7,6 are smaller than 8, which is the left subtree node of the node with a value of 8, and the last three numbers are 9,11,10 than 8, which is the right subtree node of the node with a value of 8. We then use the same method to determine the structure of the subtree corresponding to each part of the array. This is actually a recursive process. For sequence 5,7,6, the last number 6 is the value of the root node of the Zuozi. The number 5:6 is small, is the left dial hand node of the node with a value of 6, and 7 is its right child node. Similarly, the last number 10 in the sequence 9,11,10 is the root node of the right subtree, the number 9:10 is small, is the left dial hand node of the node with a value of 10, and 11 is its right child node. Let's analyze another integer array {7,4,6,5}. The last number of post-order traversal is the root node, so the value of the root node is 5. Since the first number 7 is greater than 5, there is no left subtree in the corresponding two-fork search tree, and the numbers 7,4 and 6 are the values of the right subtree nodes. But we found that the value of a node in the right subtree is 4, which is smaller than the root node value of 5, which violates the definition of a two-fork search tree. So there is no binary search tree, and the result of its post-sequential traversal is 7,4,6,5. After you find the rule, the reference code is as follows:
1 BOOLVerifysquenceofbst (intSequence[],intlength)2 {3 if(sequence==null| | length<=0)4 return false;5 6 introot=sequence[length-1];7 8 //The node of the left subtree in the binary search tree is smaller than the root node .9 intj=i;Ten for(; j<length-1;++j) One { A if(sequence[i]>root) - Break; - } the - //The nodes of the right subtree in the binary search tree are larger than the root nodes . - intj=i; - for(; j<length-1;++j) + { - if(sequence[j]<root) + return false; A } at - //determine if the left subtree is a binary search tree - BOOLleft=true; - if(i>0) -left=Verifysequenceofbst (sequence,i); - in //Judging right subtree is not binary search tree - BOOLright=true; to if(i<length-1) +Right=verifysequenceofbst (sequence+i,length-i-1); - return(left&&Right ); the}
Sequential traversal sequence of binary search tree