美文网首页
572.subtree-of-another-tree

572.subtree-of-another-tree

作者: Optimization | 来源:发表于2020-05-25 13:48 被阅读0次
    参考leetcode官方

    dfs

    class Solution {
    public:
        bool isSubtree(TreeNode* s, TreeNode* t) {
            return dfs(s, t);
        }
    private:
        bool dfs(TreeNode* s, TreeNode* t){
            if(!s) return false;
            return check(s,t) || dfs(s->left, t) || dfs(s->right, t);
        }
        bool check(TreeNode* s,TreeNode* t){
            if( !s&& !t) return true;
            if((!s && t)||(s && !t)||(s->val != t->val)) return false;
            return check(s->left,t->left) && check(s->right, t->right);         
        }
    
    };
    

    相关文章

      网友评论

          本文标题:572.subtree-of-another-tree

          本文链接:https://www.haomeiwen.com/subject/xyryahtx.html