根据扩展二叉树前序序列生成二叉树,输入某个节点的值,查找其双亲节点的值(二叉链表)

来源:互联网 发布:python编程 编辑:程序博客网 时间:2024/05/16 07:07
#include <iostream>
#include <string>
using namespace std;


struct BiNode{
char ch;
BiNode * Left;
BiNode * Right;
};
BiNode * root=NULL;
void Create(BiNode * & root)
{
char ch;
cin>>ch;
if(ch=='#')
{
root=NULL;return;
}
root=new BiNode;
root->ch=ch;
Create(root->Left);
Create(root->Right);
}
void FindParent(BiNode * & result,BiNode * root,BiNode * pre,char son)
{
if(root==NULL)
return;
if(root->ch==son)
{
result=pre;return;
}
if(!result)
FindParent(result,root->Left,root,son);
if(!result)
FindParent(result,root->Right,root,son);


}
void main()
{
Create(root);
BiNode * result=NULL;
FindParent(result,root,NULL,'A');
cout<<"Parent: "<<result->ch;
::system("pause");

}

测试输入:BC##DEA##F##G##

输出:Parent: E


原创粉丝点击