#include "stdafx.h"
#include <vector>
#include<malloc.h>
using namespace std;
class Node {
public:
Node(int i);
int k;
Node* left;
Node* right;
};
Node::Node(int i)
{
k = i;
left = NULL;
right = NULL;
}
Node* newNode[15];
Node* (*CreateTree(vector<int> vx))[15]
{
int i = 0;
for (i = 0; i < vx.size(); ++i)
{
newNode[i] = new Node(vx[i]);
}
for (i = 0; i < vx.size(); ++i)
{
if ((2 * i + 1) < vx.size())
{
newNode[i]->left = newNode[2 * i + 1];
}
else
{
return &newNode;
}
if ((2 * i + 2) < vx.size())
{
newNode[i]->right = newNode[2 * i + 2];
}
else
{
return &newNode;
}
}
}
void InPrint(Node* n)
{
if (n == NULL)
{
return;
}
InPrint(n->left);
printf("%d**", n->k);
InPrint(n->right);
}
bool ifBSTtree(Node* (*n3)[15], vector<int> v)
{
vector<int> vMax;
vector<int> vMin;
for (int i=0;i<v.size();++i)
{
vMax.push_back(v[i]);
vMin.push_back(v[i]);
}
for (int j=v.size()-1;j>0;j--)
{
int m = (j - 1) / 2;//father
if ((j % 2 == 1)&& ((*(*n3 + m))->k)>vMax[j])//left
{
vMin[m] = vMin[j];
}
else if((j%2==0)&& ((*(*n3+m))->k)<vMin[j])
{
vMax[m] = vMax[j];
}
else
{
return false;
}
}
return true;
}
int main()
{
//arrT n1;
Node* (*n2)[15];
n2 = NULL;
int i1[15] = { 10,5,15,3,8,13,20,2,4,7,9,12,14,19,50 };
vector<int> v1(i1, i1 + 15);
n2 = CreateTree(v1);
//n3 = n2;
InPrint(*n2[0]);
bool b1 = ifBSTtree(n2, v1);
printf("%d",b1);
while (1);
return 0;
}
判断一个二叉树是否是二叉搜索树
于 2022-12-14 03:31:52 首次发布