判断子树

54 阅读1分钟

public boolean isChildTree(TreeNode root1, TreeNode root2) { if (root1 == null || root2 == null) { return false; }

boolean result = false;
if (root1.val == root2.val) {
    result = helper(root1, root2);
} else if (result == false) {
    result = helper(root1.left, root2);
} else if (result == false) {
    result = helper(root1.right, root2);
}
return result;

}

public boolean helper(TreeNode p1, TreeNode p2) { if (p1 == null) { return false; } if (p2 == null) { return true; } if (p1.val != p2.val) { return false; } return helper(p1.left, p2.left) && helper(p1.right, p2.right); }