• 树的子结构


    题目描述

    输入两颗二叉树A和B,判断B是不是A的子结构,九度题目描述如下:
    题目描述:
    输入两颗二叉树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


    我的前言

    这道题在《剑指offer》里给出了具体的思路,但是他给出的二叉树数据结构定义明显无法满足九度这道题,因为我实在无法想出这里除了用结构体数组表示树还有神马其它的方法,注意我说的是用纯c实现,c++和其它语言神马的就别掺和了,这里我的二叉树结点的定义如下:

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



    思路

    A树和B树的结构如下,判断B树是否为A树的子树:



    要查找A树是否存在和B树一样的子结构,可以分为两步:
    1. 在A树中找到和B树的根结点的值一样的结点R
    2. 再判断A树中以R为根结点的子树是不是包含B树这样的子结构

    听起来就是递归的过程了

    实现(纯C)

    唉,我现在只会纯C,php,shell这三门编程语言,能基本看懂c++和java,还是要更广泛涉猎一些,先把算法和计算机系统的这些基础打牢再开始

    第一步,在A树中查找和B树根节点一样的值,其实就是树的前序遍历,建议递归,方便(ps:非递归无非就是用个栈存储结点而已,没什么技术含量,参考链接: http://blog.csdn.net/zinss26914/article/details/8450952

    /**
     * 第一步判断,遍历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;
    }


    第二步,进一步判断A中以R为根节点的子树是不是与B树具有相同的结点

    /**
     * 第二步判断,判断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));
    }




    完整代码(九度系统出问题,我没办法判断自己代码是否ac,我认为是没问题的)


    #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
    ");
    			continue;
    		}
    
    		flag = judgeChildTree(ahead, 0, bhead, 0);
    		if (flag)
    			printf("YES
    ");
    		else
    			printf("NO
    ");
    
    		free(ahead);
    		free(bhead);
    	}
    
    	return 0;
    }
    




  • 相关阅读:
    [问题2014S06] 解答
    [问题2014S07] 复旦高等代数II(13级)每周一题(第七教学周)
    [问题2014S05] 解答
    [问题2014S04] 解答
    [问题2014S06] 复旦高等代数II(13级)每周一题(第六教学周)
    [问题2014S03] 解答
    [问题2014S05] 复旦高等代数II(13级)每周一题(第五教学周)
    [问题2014S02] 解答
    [问题2014S04] 复旦高等代数II(13级)每周一题(第四教学周)
    [问题2014S01] 解答
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3165586.html
Copyright © 2020-2023  润新知