面试题26:树的子结构

面试题26:树的子结构

题目:输入两颗二叉树A和B,判断B是不是A的子结构

思路:
1.先用递归的方式判断A树中存不存在B树根节点一样的值的结点,如果不存在,则不存在该子结构;
2.在A树中找到B树根节点一样的值的结点后,通过递归的方式判断是否存在子结构。

代码:

//第一颗二叉树是否存在第二棵树根结点的值
bool hasNodeInTree1(TreeNode *root1, TreeNode *root2)
{
    bool result = false;
    if (root1 == NULL || root2 == NULL)
    {
        return false;
    }

    if (root1->value == root2->value)
    {
        result = hasTree2ContainsTree1(root1, root2);
    }

    if (!result)
    {
        result = hasNodeInTree1(root1->leftchild, root2);
    }

    if (!result)
    {
        result = hasNodeInTree1(root1->rightChild, root2);
    }

    return result;
}

//第一颗树中是否包含了第二棵树的子结构
bool hasTree2ContainsTree1(TreeNode *root1, TreeNode *root2)
{
    bool result = false;
     if (root2 == NULL)
    {
        return true;
    }

    if (root1 == NULL)
    {
        return false;
    }

    if (root1->value == root2->value)
    {
        result = hasTree2ContainsTree1(root1->leftchild, root2->leftchild) && hasTree2ContainsTree1(root1->leftchild, root2->leftchild);
    }

    return result;
}

可执行代码:

#include <iostream>
using namespace std;

typedef struct TreeNode
{
    int value;
    struct TreeNode *rightChild;
    struct TreeNode *leftchild;
} Tree, TreeNode;

//第一颗二叉树是否存在第二棵树根结点的值
bool hasNodeInTree1(TreeNode *root1, TreeNode *root2);

//第一颗树中是否包含了第二棵树的子结构
bool hasTree2ContainsTree1(TreeNode *root1, TreeNode *root2);

//添加子节点
TreeNode *addChild(TreeNode *root, int value, int direction);

int main()
{
    Tree *tree1 = new Tree();
    tree1->value = 8;
    tree1->leftchild = tree1->rightChild = NULL;

    TreeNode *sn1 = addChild(tree1, 8, -1);
    TreeNode *sn2 = addChild(tree1, 7, 1);
    TreeNode *tn1 = addChild(sn1, 9, -1);
    TreeNode *tn2 = addChild(sn1, 2, 1);
    TreeNode *fn1 = addChild(tn2, 4, -1);
    TreeNode *fn2 = addChild(tn2, 7, 1);

    Tree *tree2 = new Tree();
    tree2->value = 8;
    tree2->leftchild = tree2->rightChild = NULL;
    addChild(tree2, 9, -1);
    addChild(tree2, 2, 1);

    bool result = hasNodeInTree1(tree1, tree2);
    if (result)
    {
        cout << "has the sub structure" << endl;
    }
    else
    {
        cout << "has not the sub structure" << endl;
    }
    return 0;
}

//第一颗二叉树是否存在第二棵树根结点的值
bool hasNodeInTree1(TreeNode *root1, TreeNode *root2)
{
    bool result = false;
    if (root1 == NULL || root2 == NULL)
    {
        return false;
    }

    if (root1->value == root2->value)
    {
        result = hasTree2ContainsTree1(root1, root2);
    }

    if (!result)
    {
        result = hasNodeInTree1(root1->leftchild, root2);
    }

    if (!result)
    {
        result = hasNodeInTree1(root1->rightChild, root2);
    }

    return result;
}

//第一颗树中是否包含了第二棵树的子结构
bool hasTree2ContainsTree1(TreeNode *root1, TreeNode *root2)
{
    bool result = false;
     if (root2 == NULL)
    {
        return true;
    }

    if (root1 == NULL)
    {
        return false;
    }

    if (root1->value == root2->value)
    {
        result = hasTree2ContainsTree1(root1->leftchild, root2->leftchild) && hasTree2ContainsTree1(root1->leftchild, root2->leftchild);
    }

    return result;
}

//添加子节点
TreeNode *addChild(TreeNode *root, int value, int direction)
{
    TreeNode *node = new TreeNode();
    node->value = value;
    node->leftchild = node->rightChild = NULL;

    if (direction == 1)
    {
        root->rightChild = node;
    }
    if (direction == -1)
    {
        root->leftchild = node;
    }

    return node;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值