题解 | #NC98 判断t1树中是否有与t2树拓扑结构#
判断t1树中是否有与t2树拓扑结构完全相同的子树
http://www.nowcoder.com/practice/4eaccec5ee8f4fe8a4309463b807a542
public boolean isContains (TreeNode root1, TreeNode root2) { // write code here if(root1 == null ) return false; if(root2 == null) return true; return isContains(root1.left , root2) || isContains(root1.right, root2) || isSame(root1,root2); } /* 判断两棵树是否完全相等 */ public boolean isSame(TreeNode p, TreeNode q){ if(p == null && q == null) return true; if(p == null || q == null || p.val != q.val) return false; return isSame(p.left, q.left) && isSame(p.right , q. right); }