Two fork Search tree:(also known as: two fork find tree, binary sort tree)
Satisfying nature:
(1) It is either an empty tree;
(2) or a two-fork tree having the following properties:
<1> Joz tree is not empty, the value of all nodes on the left subtree is less than the value of its root node;
<2> If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node;
<3> left and right subtree are also two to find the order tree;
Problem Description:
Enter an array of integers to determine if the array is the result of a sequential traversal of a binary lookup tree. Returns False if True is returned.
Analytical:
According to the definition of post-order traversal, if a sequence is the result of a subsequent traversal of a two-fork tree, then it is not difficult to conclude that the last node of the sequence must be the root node of the two-fork tree, except for the root node, in which the first part of the sequence is the node of the left subtree of the two-tree, Similarly, the ergodic results of the left and right subtrees also have the same characteristics.
The code is as follows:
BOOL Verifysquenceofbst (int sequence[], int length)
{
if (sequence== NULL | | length <= 0)
Returnfalse;
int root =sequence[length-1];
The node of the left subtree in the binary search tree is smaller than the root node.
int i = 0;
for (; I <length-1; + + i)
{
if (Sequence[i] > root)
Break
}
The nodes of the right subtree in the binary search tree are larger than the root nodes.
int j = i;
for (; J <length-1; + + j)
{
if (Sequence[j] < root)
return false;
}
Determine if the left subtree is a binary search tree
BOOL left =true;
if (i > 0)
Left =verifysquenceofbst (sequence, i);
Judging right subtree is not binary search tree
bool Right =true;
if (i <length-1)
Right =verifysquenceofbst (sequence + i, length-i-1);
Return (left&& right);
}
Note: "Sword refers to offer" study notes
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Sequential traversal of a 8:2-fork search Tree