pta-子孙关系判断 (50 分)
作者:互联网
已知二叉树的先序序列,判断结点u是否是结点v的子孙,是就输出v到u的路径长度,否则输出NO。假设结点个数少于50个。
输入格式:
输入共二行,第一行中给出先序序列,第二行给出两个顶点。*表示空树。
输出格式:
输出一个整数或NO。
输入样例1:
ABC**DE*G**F***
BE
结尾无空行
输出样例1:
2
结尾无空行
本人用的是C++模版写的,但是这并不能够影响本题的本质,所以,仅看代码内容即可。
//创建一棵二叉树(先序顺序)
//构造函数
template <class ElemType>
BiNode<ElemType> *BiTree<ElemType>::Creat(BiNode<ElemType> *bt){
ElemType ch;
// cout << "请输入创建一棵二叉树的节点数据:" << endl;
cin >> ch;
if (ch == '*'){
return NULL;
}
else{
bt = new BiNode<ElemType>;
bt->data = ch;
bt->lchild = Creat(bt->lchild);
bt->rchild = Creat(bt->rchild);
}
return bt;
}
//找到首节点,并且计算路径
//计算路径
int length = -1; //这里是全局变量
template <class ElemType>
int BiTree<ElemType>::CountPath(BiNode<ElemType> *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 <class ElemType>
int BiTree<ElemType>::FindTree(BiNode<ElemType> *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 <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
template <class ElemType>
struct BiNode{
ElemType data;
BiNode <ElemType> *lchild,*rchild;
};
int length = -1;
template<class ElemType>
class BiTree{
private:
BiNode<ElemType> *root;
BiNode<ElemType> *Creat(BiNode<ElemType> *bt);
void Release(BiNode<ElemType> *bt);
void InOrder(BiNode<ElemType> *bt);
int Depth(BiNode<ElemType> *bt);
int FindTree(BiNode<ElemType> *bt,ElemType data1,ElemType data2);
int CountPath(BiNode<ElemType> *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 <class ElemType>
int BiTree<ElemType>::CountPath(BiNode<ElemType> *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 <class ElemType>
int BiTree<ElemType>::FindTree(BiNode<ElemType> *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 <class ElemType>
BiNode<ElemType> *BiTree<ElemType>::Creat(BiNode<ElemType> *bt){
ElemType ch;
// cout << "请输入创建一棵二叉树的节点数据:" << endl;
cin >> ch;
if (ch == '*'){
return NULL;
}
else{
bt = new BiNode<ElemType>;
bt->data = ch;
bt->lchild = Creat(bt->lchild);
bt->rchild = Creat(bt->rchild);
}
return bt;
}
//析构函数
template <class ElemType>
void BiTree<ElemType>::Release(BiNode<ElemType> *bt){
if (bt != NULL) {
Release(bt->lchild);
Release(bt->rchild);
delete bt;
}
}
//求二叉树的深度
template <class ElemType>
int BiTree<ElemType>::Depth(BiNode<ElemType> *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 <class Elemtype>
void BiTree<Elemtype>::InOrder(BiNode<Elemtype> *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<char> T;
cin >> tr1 >>tr2 ;
if(T.CountPath(tr1, tr2)){
cout << length ;
}
else cout << "NO";
return 0;
}
标签:BiNode,return,子孙,50,pta,bt,length,root,data2 来源: https://blog.csdn.net/qq_51802524/article/details/121139421