class Solution { public boolean isSubStructure(TreeNode A, TreeNode B) { if(A==null || B==null) return false; if(A.val != B.val){ return isSubStructure(A.left,B) ||isSubStructure(A.right,B); }else{ return isMatched(A,B)||isSubStructure(A.left,B) ||isSubStructure(A.right,B); } } private boolean isMatched(TreeNode a, TreeNode b) { if(b!=null){ if(a==null) return false; else{ if(b.val == a.val){ return isMatched(a.left,b.left) && isMatched(a.right,b.right); } } }else{ return true; } return false; } }