/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */classSolution{publicbooleanisSubStructure(TreeNodeA,TreeNodeB){return(A!=null&&B!=null)&&(doesAHaveB(A,B)||isSubStructure(A.left,B)||isSubStructure(A.right,B));}//判断B是否是A的子树 //先判断根节点是否相同 如果不同直接返回false//如果相同再递归判断左右子树publicbooleandoesAHaveB(TreeNodeA,TreeNodeB){if(B==null){returntrue;}if(A==null){returnfalse;}if(A.val!=B.val){returnfalse;}returndoesAHaveB(A.left,B.left)&&doesAHaveB(A.right,B.right);}}