题解 | #判断t1树中是否有与t2树完全相同的子树#

判断t1树中是否有与t2树完全相同的子树

http://www.nowcoder.com/practice/4eaccec5ee8f4fe8a4309463b807a542

/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param root1 TreeNode类 
     * @param root2 TreeNode类 
     * @return bool布尔型
     */
    bool isContains(TreeNode* root1, TreeNode* root2) {
        // write code here
        if(root1 == nullptr && root2 == nullptr){
            return true;
        }
        if(root1 == nullptr || root2 == nullptr){
            return false;
        }
        
        if(root1->val == root2->val){
            return isContains(root1->left, root2->left) && isContains(root1->right, root2->right);
        }
        
        return isContains(root1->left, root2) || isContains(root1->right, root2);
        
    }
};
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务