publicbooleanisSubStructure(TreeNode A, TreeNode B) { if (B == null) { returnfalse; } return isContained(A, B) || (A != null && (isSubStructure(A.left, B) || isSubStructure(A.right, B))); }
privatebooleanisContained(TreeNode A, TreeNode B) { // B 遍历结束,说明 B 是 A 的子结构 if (B == null) { returntrue; } if (A == null) { returnfalse; } if (A.val != B.val) { returnfalse; } return isContained(A.left, B.left) && isContained(A.right, B.right); }