已知二叉树的先序序列,判断结点u是否是结点v的子孙,是就输出v到u的路径长度,否则输出NO。假设结点个数少于50个。
输入格式:
输入共二行,第一行中给出先序序列,第二行给出两个顶点。*表示空树。
输出格式:
输出一个整数或NO。
输入样例1:
ABC**DE*G**F***
BE
结尾无空行
输出样例1:
2
结尾无空行
本人用的是C++模版写的,但是这并不能够影响本题的本质,所以,仅看代码内容即可。
//创建一棵二叉树(先序顺序)
//构造函数 templateBiNode *BiTree ::Creat(BiNode *bt){ ElemType ch; // cout << "请输入创建一棵二叉树的节点数据:" << endl; cin >> ch; if (ch == '*'){ return NULL; } else{ bt = new BiNode ; bt->data = ch; bt->lchild = Creat(bt->lchild); bt->rchild = Creat(bt->rchild); } return bt; }
//找到首节点,并且计算路径
//计算路径 int length = -1; //这里是全局变量 templateint BiTree ::CountPath(BiNode *root, ElemType data2){ if (root == NULL) { length++; return 0; } else{ length++; if (root->data == data2) { return length; } if(!CountPath(root->lchild, data2)) length--; else return length; if(!CountPath(root->rchild, data2)) length--; else return length; return 0; } } //找到首节点 template int BiTree ::FindTree(BiNode *bt, ElemType data1, ElemType data2){ if (bt == NULL) { return 0; } else{ if (bt->data == data1) { return CountPath(bt, data2); } if(FindTree(bt->lchild, data1, data2)) return length; if(FindTree(bt->rchild, data1, data2)) return length; } return 0; }
下面附上所有的代码
#include#include #include using namespace std; template struct BiNode{ ElemType data; BiNode *lchild,*rchild; }; int length = -1; template class BiTree{ private: BiNode *root; BiNode *Creat(BiNode *bt); void Release(BiNode *bt); void InOrder(BiNode *bt); int Depth(BiNode *bt); int FindTree(BiNode *bt,ElemType data1,ElemType data2); int CountPath(BiNode *root,ElemType data2); public: BiTree(){root = Creat(root);} ~BiTree(){ Release(root); } void InOrder(){ InOrder(root); } int Depth(){ return Depth(root); } int CountPath(ElemType data1,ElemType data2){ return FindTree(root, data1, data2); } }; //寻找并计算路径 template int BiTree ::CountPath(BiNode *root, ElemType data2){ if (root == NULL) { length++; return 0; } else{ length++; if (root->data == data2) { return length; } if(!CountPath(root->lchild, data2)) length--; else return length; if(!CountPath(root->rchild, data2)) length--; else return length; return 0; } } //找到首节点 template int BiTree ::FindTree(BiNode *bt, ElemType data1, ElemType data2){ if (bt == NULL) { return 0; } else{ if (bt->data == data1) { return CountPath(bt, data2); } if(FindTree(bt->lchild, data1, data2)) return length; if(FindTree(bt->rchild, data1, data2)) return length; } return 0; } //构造函数 template BiNode *BiTree ::Creat(BiNode *bt){ ElemType ch; // cout << "请输入创建一棵二叉树的节点数据:" << endl; cin >> ch; if (ch == '*'){ return NULL; } else{ bt = new BiNode ; bt->data = ch; bt->lchild = Creat(bt->lchild); bt->rchild = Creat(bt->rchild); } return bt; } //析构函数 template void BiTree ::Release(BiNode *bt){ if (bt != NULL) { Release(bt->lchild); Release(bt->rchild); delete bt; } } //求二叉树的深度 template int BiTree ::Depth(BiNode *bt){ if (bt == NULL) { return 0; } else{ int dep1 = Depth(bt->lchild); int dep2 = Depth(bt->rchild); return (dep1>dep2)?(dep1+1):(dep2+1); } } //中序遍历 template void BiTree ::InOrder(BiNode *bt){ if(bt==NULL){ return; } else{ InOrder(bt->lchild); cout << bt->data << " "; InOrder(bt->rchild); } } int main(int argc, const char * argv[]) { char tr1,tr2; BiTree T; cin >> tr1 >>tr2 ; if(T.CountPath(tr1, tr2)){ cout << length ; } else cout << "NO"; return 0; }



