二叉树中,寻找离两个结点最近的公共祖先结点

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值