Sequential traversal of a 8:2-fork search Tree

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.