classSolution { publicbooleanisSubStructure(TreeNode A, TreeNode B) { if (B == null) { returnfalse; }
// B is not null if (A == null) { returnfalse; }
return contains(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B); }
privatebooleancontains(TreeNode A, TreeNode B) { if (B == null) { // 注意 B == null 即可返回 true, 无需 A == null, 因为是包含子树,即 A 及子节点可能都不为空,B 树只是 A 树的一部分 returntrue; }
// B is not null if (A == null || A.val != B.val) { returnfalse; }