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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值