题解 | #树的子结构#

树的子结构

https://www.nowcoder.com/practice/6e196c44c7004d15b1610b9afca8bd88

public class Solution {
    public boolean HasSubtree(TreeNode root1TreeNode root2) {

        if (root2 == null)return false;
        if (root1 == null)return false;
        boolean flag = isLike(root1, root2);
        boolean next = HasSubtree(root1.left, root2);
        boolean next2 = HasSubtree(root1.right, root2);
        return flag || next || next2;
    }
    public boolean isLike(TreeNode aTreeNode b) {
        if (b == null)return true;
        if (a == null || a.val != b.val)return false;
        return isLike(a.leftb.left) && isLike(a.rightb.right);
    }
}
全部评论

相关推荐

点赞 评论 收藏
分享
猫头夜鹰:图书管理系统能有面试就怪了,放十年前都不行
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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