本文共 431 字,大约阅读时间需要 1 分钟。
递归啊递归,一看就会,一写就废。 思路:1.遍历A树2.看以A树的每一个节点为根节点的树是不是包含B
class Solution { public boolean isSubStructure(TreeNode A, TreeNode B) { return (A!=null&&B!=null)&&(rec(A,B)||isSubStructure(A.left,B)||isSubStructure(A.right,B)); } public boolean rec(TreeNode A,TreeNode B){ if(B==null) return true; if(A==null||A.val!=B.val) return false; return rec(A.right,B.right)&&rec(A.left,B.left); }}
转载地址:http://vsnwi.baihongyu.com/