Skip to content

Latest commit

 

History

History
20 lines (17 loc) · 631 Bytes

24.md

File metadata and controls

20 lines (17 loc) · 631 Bytes

SubTree of another Tree

LeetCode Link

    bool isSameTree(TreeNode* root, TreeNode* subRoot){
        if(root==nullptr&&subRoot==nullptr) return true;
        if(root==nullptr||subRoot==nullptr) return false;
        if(root->val!=subRoot->val) return false;

        return isSameTree(root->left,subRoot->left)&&isSameTree(root->right,subRoot->right);
    }
    bool isSubtree(TreeNode* root, TreeNode* subRoot) {
        if(root==nullptr){
            return false;
        }
        if(isSameTree(root,subRoot)) return true;

        return isSubtree(root->left,subRoot)||isSubtree(root->right,subRoot);
    }