列表

详情


NC273. 树的子结构

描述

输入两棵二叉树A,B,判断B是不是A的子结构。(我们约定空树不是任意一个树的子结构)
假如给定A为{8,8,7,9,2,#,#,#,#,4,7},B为{8,9,2},2个树的结构如下,可以看出B是A的子结构

数据范围:
0 <= A的节点个数 <= 10000
0 <= B的节点个数 <= 10000

示例1

输入:

{8,8,7,9,2,#,#,#,#,4,7},{8,9,2}

输出:

true

示例2

输入:

{1,2,3,4,5},{2,4}

输出:

true

示例3

输入:

{1,2,3},{3,1}

输出:

false

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 4ms, 内存消耗: 1312KB, 提交时间: 2022-02-08

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    bool isChild(TreeNode* pRoot1, TreeNode* pRoot2)
    {
        if (pRoot2 == nullptr)
            return true;
        if (pRoot1 == nullptr)
            return false;
        if (pRoot1->val != pRoot2->val)
            return false;
        return isChild(pRoot1->left, pRoot2->left) && isChild(pRoot1->right, pRoot2->right);
    }
    bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {
        if(pRoot1 == nullptr || pRoot2 == nullptr)
            return false;
        if (pRoot1->val == pRoot2->val)
            if (isChild(pRoot1, pRoot2))
            {
                return true;
            }
        return HasSubtree(pRoot1->left, pRoot2) || HasSubtree(pRoot1->right, pRoot2);
    }
};

C++ 解法, 执行用时: 4ms, 内存消耗: 1324KB, 提交时间: 2022-02-08

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
     bool isSubtree(TreeNode* pRootA, TreeNode* pRootB) {
        if (pRootB == NULL) return true;
        if (pRootA == NULL) return false;
        if (pRootB->val == pRootA->val) {
            return isSubtree(pRootA->left, pRootB->left)
                && isSubtree(pRootA->right, pRootB->right);
        } else return false;
    }
public:
    bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {
    if (pRoot1 == NULL || pRoot2 == NULL) return false;
        return isSubtree(pRoot1, pRoot2) ||
            HasSubtree(pRoot1->left, pRoot2) ||
            HasSubtree(pRoot1->right, pRoot2);
    }
};

C++ 解法, 执行用时: 4ms, 内存消耗: 1328KB, 提交时间: 2021-10-31

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {
        if(!pRoot2 || !pRoot1) return false;
        bool result = false;
        //先序遍历
        result = Istreeboy(pRoot1, pRoot2);
        
        if(!result) result = HasSubtree(pRoot1->left, pRoot2);
        if(!result) result = HasSubtree(pRoot1->right, pRoot2);
        
        return result;
    }
    
    bool Istreeboy(TreeNode* pRoot1, TreeNode* pRoot2)
    {
        if(!pRoot2) return true;
        if(!pRoot1) return false;
        //if(!pRoot2) return true;
        if(pRoot1->val == pRoot2->val)
        {
            return Istreeboy(pRoot1->left, pRoot2->left) and Istreeboy(pRoot1->right, pRoot2->right);
        }
        return false;
    }
};

C++ 解法, 执行用时: 4ms, 内存消耗: 1332KB, 提交时间: 2022-02-10

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    bool is(TreeNode* p1,TreeNode*p2)
    {
        if(p2==nullptr)return true;//说明 匹配成功了
        if(p1==nullptr)return false;// 匹配失败
        if(p1->val!=p2->val)return false;
        return is(p1->left,p2->left)&&is(p1->right,p2->right);
    }
    bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {
         if(pRoot1==nullptr||pRoot2==nullptr)return false;
         //找到判断入口
        bool ret=false;
        if(pRoot1->val==pRoot2->val)
        {
             ret=is(pRoot1,pRoot2);
        }
        if(ret!=true)
        {
            ret=HasSubtree(pRoot1->left,pRoot2);
        }
        if(ret!=true)
        {
            ret=HasSubtree(pRoot1->right,pRoot2);
        }
        return ret;
    }
};

C++ 解法, 执行用时: 4ms, 内存消耗: 1336KB, 提交时间: 2022-02-09

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {
        if (!pRoot1 || !pRoot2) return false;
        if (isPart(pRoot1, pRoot2)) return true;
        return HasSubtree(pRoot1->left, pRoot2) || HasSubtree(pRoot1->right, pRoot2);
    }
    
    bool isPart(TreeNode* p1, TreeNode* p2){
        if (!p2) return true;
        if (!p1 || p1->val != p2->val) return false;
        return isPart(p1->left, p2->left) && isPart(p1->right, p2->right);
    }
};

上一题