#include<iostream>
#include<stdlib.h>
using namespace std;
struct TreeNode {
int data;
TreeNode *leftChild,*rightChild;
TreeNode()
{
leftChild = NULL;
rightChild = NULL;
}
};
class BinaryTree {
protected:
TreeNode *root;
void copy(TreeNode *&target,TreeNode *origin);
void create(TreeNode *&root,const int *a,int i,const int l);
void destroy(TreeNode *&root);
TreeNode *parent(TreeNode* root,TreeNode *current);
bool Find(TreeNode *root,const int &x,TreeNode *&tn);
bool Issub(TreeNode *root,TreeNode *p);
public:
BinaryTree() { root=NULL; }
~BinaryTree() { destroy(root); }
void Create(BinaryTree *&bt,const int *a,int i,const int l);
TreeNode *getRoot() { return root; }
void preOrder(TreeNode *root);
TreeNode *Parent(TreeNode *current);
TreeNode *Find(const int &x);
void Ancestor(TreeNode *root,TreeNode *p,TreeNode *q,TreeNode *&r);
};
void BinaryTree::create(TreeNode *&root,const int *a,int i,const int l)
{
if (i>=l)
return;
if (a[i]==0) {
root = NULL;
return;
}
else {
root = new TreeNode();
root->data = a[i];
create(root->leftChild,a,i*2+1,l);
create(root->rightChild,a,i*2+2,l);
}
}
void BinaryTree::Create(BinaryTree *&bt,const int *a,int i,const int l)
{
this->create(bt->root,a,i,l);
}
TreeNode* BinaryTree::Parent(TreeNode *current)
{
if (current==NULL||current==root)
return NULL;
return this->parent(this->root,current);
}
void BinaryTree::destroy(TreeNode *&root)
{
if (root->leftChild!=NULL)
destroy(root->leftChild);
if (root->rightChild!=NULL)
destroy(root->rightChild);
delete root;
root = NULL;
}
TreeNode* BinaryTree::parent(TreeNode* root,TreeNode *current)
{
if (root==NULL)
return NULL;
if (root->leftChild==current||root->rightChild==current)
return root;
else return (parent(root->leftChild,current)==NULL)?parent(root->rightChild,current):parent(root->leftChild,current);
}
void BinaryTree::preOrder(TreeNode *root)
{
if (root!=NULL)
{
cout << root->data << " ";
preOrder(root->leftChild);
preOrder(root->rightChild);
}
}
TreeNode *BinaryTree::Find(const int &x)
{
TreeNode *tn;
if (this->Find(this->root,x,tn))
return tn;
else return NULL;
}
bool BinaryTree::Find(TreeNode *root,const int &x,TreeNode *&tn)
{
if(root==NULL)
return false;
if (x==root->data) {
tn = root;
return true;
}
if (Find(root->leftChild,x,tn)||Find(root->rightChild,x,tn))
return true;
else return false;
}
//write your code here
bool BinaryTree::Issub(TreeNode *root,TreeNode *p)
{
if(root==NULL)return false;
if(root==p)
return true;
if(Issub(root->leftChild,p)||Issub(root->rightChild,p))
return true;
else
return false;
}
void BinaryTree::Ancestor(TreeNode *root,TreeNode *p,TreeNode *q,TreeNode *&r)
{
if(root==p||root==q)
{
r=root;
return ;
}
if(Issub(root->leftChild,p)&&Issub(root->rightChild,q)||Issub(root->leftChild,q)&&Issub(root->rightChild,p))
{
r=root;
return ;
}
else if(Issub(root->leftChild,p)&&Issub(root->leftChild,q))
Ancestor(root->leftChild,p,q,r);
else
Ancestor(root->rightChild,p,q,r);
}
int main()
{
int length = 10;
cin >> length;
int *a = new int[length];
int i;
for (i=0;i<length;i++)
cin >> a[i];
BinaryTree *bt = new BinaryTree();
bt->Create(bt,a,0,length);
TreeNode *p,*q;
TreeNode *r;
int m,n;
cin >> m >> n;
p = bt->Find(m);
q = bt->Find(n);
bt->Ancestor(bt->getRoot(),p,q,r);
cout << r->data << endl;
return 0;
}
二叉树中,寻找离两个结点最近的公共祖先结点
最新推荐文章于 2022-12-04 09:36:41 发布