题解 | #判断t1树中是否有与t2树完全相同的子树#
判断t1树中是否有与t2树完全相同的子树
https://www.nowcoder.com/practice/4eaccec5ee8f4fe8a4309463b807a542
using System; using System.Collections.Generic; /* public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode (int x) { val = x; } } */ class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root1 TreeNode类 * @param root2 TreeNode类 * @return bool布尔型 */ public bool isContains (TreeNode root1, TreeNode root2) { // write code here if (root1 == null && root2 == null) return true; if (root1 == null || root2 == null) return false; if (root1.val.Equals(root2.val)) return isSame(root1, root2); return isContains(root1.left, root2) || isContains(root1.right, root2); } public bool isSame (TreeNode root1, TreeNode root2) { // write code here if (root1 == null && root2 == null) return true; if (root1 == null || root2 == null) return false; if (!root1.val.Equals(root2.val)) return false; return isContains(root1.left, root2.left) && isContains(root1.right, root2.right); } }