【剑指Offer面试题】 九度OJ1520:树的子结构

题目链接地址:
http://ac.jobdu.com/problem.php?pid=1520

题目1520:树的子结构

时间限制:1 秒内存限制:128 兆特殊判题:否提交:1595解决:380
题目描述:
输入两颗二叉树A,B,判断B是不是A的子结构。
输入:
输入可能包含多个测试样例,输入以EOF结束。
对于每个测试案例,输入的第一行一个整数n,m(1<=n<=1000,1<=m<=1000):n代表将要输入的二叉树A的节点个数(节点从1开始计数),m代表将要输入的二叉树B的节点个数(节点从1开始计数)。接下来一行有n个数,每个数代表A树中第i个元素的数值,接下来有n行,第一个数Ki代表第i个节点的子孩子个数,接下来有Ki个树,代表节点i子孩子节点标号。接下来m+1行,与树A描述相同。
输出:
对应每个测试案例,
若B是A的子树输出”YES”(不包含引号)。否则,输出“NO”(不包含引号)。
样例输入:
7 3
8 8 7 9 2 4 7
2 2 3
2 4 5
0
0
2 6 7
0
0
8 9 2
2 2 3
0
0

1 1
2
0
3
0
样例输出:
YES
NO
提示:
B为空树时不是任何树的子树。


思路分析:

  1. 先构造二叉树A和二叉树B;
  2. 依次遍历二叉树A的每个结点rootA,与二叉树B的根结点rootB进行比较;
  3. 如果相等则同时前序遍历以rootA为根结点的二叉树subA和二叉树B。断定B树是不是subA树的子树,不是的话提前终止遍历过程,跳转到步骤2继续遍历二叉树A的下一个结点,直到结点遍历完。
    不过题目中的输入输出方式有点问题。参考http://blog.csdn.net/wzy_1988/article/details/9209899
    需要改用数组作为存储二叉树节点的数据结构。

代码

#include <stdio.h>
#include <stdlib.h>

// 二叉树结点定义
struct btree
{
    int value;
    int lchild, rchild;
};

// A树和B树的最多结点数
int n, m;

/**
 * 第二步判断,判断A树是否有B树的子结构
 */
int doesTree1HasTree2(struct btree *ahead, int numa, struct btree *bhead, int numb)
{
    if (numb == -1) 
        return 1;
    if (numa == -1)
        return 0;

    if (ahead[numa].value != bhead[numb].value)
        return 0;

    return (doesTree1HasTree2(ahead, ahead[numa].lchild, bhead, bhead[numb].lchild) &&
        doesTree1HasTree2(ahead, ahead[numa].rchild, bhead, bhead[numb].rchild));
}

/**
 * 第一步判断,遍历A树查找是否有等于B树根结点的子树
 */
int judgeChildTree(struct btree *ahead, int numa, struct btree *bhead, int numb)
{
    int flag = 0;

    if (numa != -1 && numb != -1) {
        if (ahead[numa].value == bhead[numb].value)
            flag = doesTree1HasTree2(ahead, numa, bhead, numb);

        if (! flag && ahead[numa].lchild != -1)
            flag = judgeChildTree(ahead, ahead[numa].lchild, bhead, numb);

        if (! flag && ahead[numa].rchild != -1)
            flag = judgeChildTree(ahead, ahead[numa].rchild, bhead, numb);
    }

    return flag;
}

int main(void)
{
    int i, data, count, left, right, flag;
    struct btree *ahead, *bhead;

    while (scanf("%d %d", &n, &m) != EOF) {
        // 获取A树的节点值
        ahead = (struct btree *)malloc(sizeof(struct btree) * n);
        for (i = 0; i < n; i ++) {
            scanf("%d", &data);
            ahead[i].value = data;
            ahead[i].lchild = ahead[i].rchild = -1;
        }

        for (i = 0; i < n; i ++) {
            scanf("%d", &count);
            if (count == 0) {
                continue;
            } else {
                if (count == 1) {
                    scanf("%d", &left);
                    ahead[i].lchild = left - 1;
                } else {
                    scanf("%d %d", &left, &right);
                    ahead[i].lchild = left - 1;
                    ahead[i].rchild = right - 1;
                }
            }
        }

        // 获取B树的节点值
        bhead = (struct btree *)malloc(sizeof(struct btree) * m);
        for (i = 0; i < m; i ++) {
            scanf("%d", &data);
            bhead[i].value = data;
            bhead[i].lchild = bhead[i].rchild = -1;
        }

        for (i = 0; i < m; i ++) {
            scanf("%d", &count);
            if (count == 0) {
                continue;
            } else {
                if (count == 1) {
                    scanf("%d", &left);
                    bhead[i].lchild = left - 1;
                } else {
                    scanf("%d %d", &left, &right);
                    bhead[i].lchild = left - 1;
                    bhead[i].rchild = right - 1;
                }
            }
        }

        // 判断B树是否为A的子树
        if (n == 0 || m == 0) {
            printf("NO\n");
            continue;
        }

        flag = judgeChildTree(ahead, 0, bhead, 0);
        if (flag)
            printf("YES\n");
        else
            printf("NO\n");

        free(ahead);
        free(bhead);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值