guanpengchn / guanpengchn.github.io

:memo: code on DEV branch, blogs on ISSUES
https://guanpengchn.github.io
18 stars 9 forks source link

树的子结构 #41

Open guanpengchn opened 5 years ago

guanpengchn commented 5 years ago

题目描述

输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

/*
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)
    {
        bool result = false;
        if(pRoot1 != NULL && pRoot2!=NULL){
            if(pRoot1->val == pRoot2->val){
                result = isChild(pRoot1, pRoot2);
            }
            if(!result){
                result = isChild(pRoot1->left, pRoot2);
            }
            if(!result){
                result = isChild(pRoot1->right, pRoot2);
            }
        }
        return result;
    }

    bool isChild(TreeNode* pRoot1, TreeNode* pRoot2){
        if(pRoot2 == NULL){
            return true;
        }
        if(pRoot1 == NULL){
            return false;
        }
        if(pRoot1->val != pRoot2->val){
            return false;
        }
        return isChild(pRoot1->left, pRoot2->left) && isChild(pRoot1->right, pRoot2->right);

    }
};