题目
代码
class Solution {
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
return (root != null) && (subRoot != null) &&((helper(root, subRoot) || isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot)));
}
public boolean helper(TreeNode A, TreeNode B) {
if (A == null && B == null) return true;
if (A == null || B == null || A.val != B.val) return false;
return helper(A.left,B.left) && helper(A.right,B.right);
}
}
注:《与剑指 Offer 26. 树的子结构》,这道题很像,注意区别。