剑指offer——二叉树

本文介绍了一种用于判断一棵二叉树是否为另一棵二叉树子树的算法。通过递归比较两棵树的节点值,实现对子树的精确匹配。文章详细展示了创建二叉树节点、连接节点、打印树结构以及销毁树等操作,并提供了完整的C++代码实现。
#include"iostream"
#include"stdio.h"
#include"math.h"
using namespace std;

struct BinaryTreeNode
{
    double m_Value;
    BinaryTreeNode* m_pLeft;
    BinaryTreeNode* m_pRight;
};

BinaryTreeNode* CreateBinaryTreeNode(double value)
{
    BinaryTreeNode* pNode=new BinaryTreeNode();
    pNode->m_Value=value;
    pNode->m_pLeft=nullptr;
    pNode->m_pRight=nullptr;

    return pNode;
}

void ConnectTreeNodes(BinaryTreeNode* pParent,BinaryTreeNode* pLeft,BinaryTreeNode* pRight)
{
    if(pParent!=nullptr)
    {
        pParent->m_pLeft=pLeft;
        pParent->m_pRight=pRight;
    }
}

void PrintTreeNode(const BinaryTreeNode* pNode)
{
    if(pNode!=nullptr)
    {
        cout<<"value of this node is:"<<pNode->m_Value<<endl;

        if(pNode->m_pLeft!=nullptr)
            cout<<"value of its left child is:"<<pNode->m_pLeft->m_Value<<endl;
        else
            cout<<"left child is nullptr."<<endl;
        if(pNode->m_pRight!=nullptr)
            cout<<"value of its right child is:"<<pNode->m_pRight->m_Value<<endl;
        else
            cout<<"right child is nullptr."<<endl;
    }
    else
        cout<<"this node is nullptr."<<endl;
    cout<<endl;
}

void PrintTree(const BinaryTreeNode* pRoot)
{
    PrintTreeNode(pRoot);

    if(pRoot!=nullptr)
    {
        if(pRoot->m_pLeft!=nullptr)
            PrintTreeNode(pRoot->m_pLeft);

        if(pRoot->m_pRight!=nullptr)
            PrintTreeNode(pRoot->m_pRight);
    }
}

void DestroyTree(BinaryTreeNode* pRoot)
{
    if(pRoot!=nullptr)
    {
        BinaryTreeNode* pLeft=pRoot->m_pLeft;
        BinaryTreeNode* pRight=pRoot->m_pRight;

        delete pRoot;
        pRoot=nullptr;

        DestroyTree(pLeft);
        DestroyTree(pRight);
    }
}

bool Equal(const double &a,const double &b)
{
    if(fabs(a-b)<0.0000001)
        return true;
    return false;
}

bool DoesTreeAHaveTreeB(BinaryTreeNode* pRootA,BinaryTreeNode* pRootB)
{
    if(pRootB==nullptr)
        return true;
    if(pRootA==nullptr)
        return false;

    if(Equal(pRootA->m_Value,pRootB->m_Value))
    {
       return DoesTreeAHaveTreeB(pRootA->m_pLeft,pRootB->m_pLeft)&&DoesTreeAHaveTreeB(pRootA->m_pRight,pRootB->m_pRight);
    }
    else
    {
        return false;
    }
}
bool HasSubTree(BinaryTreeNode* pRootA,BinaryTreeNode* pRootB)
{
    if(pRootB==nullptr)
        return false;
    if(pRootA==nullptr)
        return false;

    bool result=false;

    if(Equal(pRootA->m_Value,pRootB->m_Value))
    {
        result=DoesTreeAHaveTreeB(pRootA,pRootB);
    }
    if(!result)
    {
        result=HasSubTree(pRootA->m_pLeft,pRootB);
    }
    if(!result)
    {
        result=HasSubTree(pRootA->m_pRight,pRootB);
    }

    return result;
}
BinaryTree.h

 

转载于:https://www.cnblogs.com/acm-jing/p/10425571.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值