Sword Finger Offer Interview Question 26. The substructure of the tree [medium]

Interview Question 26. The substructure of the tree

https://leetcode-cn.com/problems/shu-de-zi-jie-gou-lcof/submissions/

Note the order of the two ifs in the func() function

class Solution {
public:
    bool isSubStructure(TreeNode* A, TreeNode* B) {
        if(!A || !B)    return false;
        return func(A,B) || isSubStructure(A->left,B)|| isSubStructure(A->right,B);
    }
    bool func(TreeNode*a,TreeNode*b){
        if(!b)  return true;
        if(!a)  return false;
        return a->val == b->val && func(a->left,b->left) &&func(a->right,b->right);   
    }
};

Guess you like

Origin blog.csdn.net/qq_41041762/article/details/105883975