题解|#判断t1树中是否有与t2树完全相同的子树#|Go
判断t1树中是否有与t2树完全相同的子树
https://www.nowcoder.com/practice/4eaccec5ee8f4fe8a4309463b807a542
package main /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root1 TreeNode类 * @param root2 TreeNode类 * @return bool布尔型 */ func isContains( root1 *TreeNode , root2 *TreeNode ) bool { if root1 == nil && root2 == nil { return true } if root1 == nil || root2 == nil { return false } if dfs(root1, root2) { return true } return isContains(root1.Left, root2) || isContains(root1.Right, root2) } func dfs(tree1 *TreeNode, tree2 *TreeNode) bool { if tree1 == nil && tree2 == nil { return true } if tree1 == nil || tree2 == nil { return false } if tree1.Val != tree2.Val { return false } return dfs(tree1.Left, tree2.Left) && dfs(tree1.Right, tree2.Right) }