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,11,10,8} returns true because the integer sequence is the result of a post-order traversal of a two-fork tree. If the input array is {7,4,6,5}, it returns false because no subsequent traversal of the binary search tree is the result of this sequence.
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.
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 digits 5,7 and 6 are smaller than 8, which is the left subtree node of the node with a value of 8, and the last 3 digits 9,11 and 10 are larger 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, in the sequence 9,11,10, the last Number 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 examine another array {7,4,6,5}. The last tree in the post-order traversal is the root node, so the value of the root node is 5. Because the first number is 7:5 large, the corresponding two-fork search tree, the root node is no left subtree, the number 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 value of the root node 5, which violates the definition of a two-fork search tree. Therefore there is no binary search tree, and the result of its post-sequential traversal is 7,4,6,5.
Implemented in Java code;
/** * Enter an array 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 */package swordforoffer;/** * @author Jinshuangqi * * August 4, 2015 */public class E24sequenceofbst {Publi C Boolean verifysequence (int[] array,int Start,int end) throws exception{if (array = = NULL | | Array.Length < 2) return TR Ue;if (Start < 0) {throw new Exception ("First can ' t is less than 0");} if (End > Array.Length) {throw new Exception ("Last can ' t is greater than the count of the element."); int root = array[end];//The node of the left subtree in the binary search tree is smaller than the root node int i = Start;for (; i < end;i++) {if (array[i]>root) break;} The node of the right subtree in the binary search tree is greater than the root node int j = I;for (; j < end;j++) {if (Array[j] < root) return false;} Determine if the left subtree is a binary search tree that is a Boolean right = True;if (i >start) to =verifysequence (array, start,i-1);//Determine if a subtree is a binary search tree. = True;if (i < end) right = Verifysequence (array,i,end-1), return (left && right);} public static void Main (string[] args) throws exception{int[] arr={5,7,6,9,11,10,8}; E24sequenceofbst test = new E24sequenceOfbst (); System.out.println (test.verifysequence (arr, 0, arr.length-1));}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Sword Point offer Surface Test (Java Edition): Two-fork search tree sequence