/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ classSolution{ publicbooleanisSubStructure(TreeNode A, TreeNode B){ if (A == null || B == null) returnfalse; boolean result = hasSubStructure(A, B); result = result || isSubStructure(A.left, B); result = result || isSubStructure(A.right, B); return result; }
publicbooleanhasSubStructure(TreeNode A, TreeNode B){ if (B == null) returntrue; if (A == null || A.val != B.val) returnfalse; if (!hasSubStructure(A.left, B.left)) returnfalse; if (!hasSubStructure(A.right, B.right)) returnfalse; returntrue; } }