1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// @Title: 树的子结构 (树的子结构  LCOF)
// @Author: 15816537946@163.com
// @Date: 2022-02-12 10:47:17
// @Runtime: 8 ms
// @Memory: 6.8 MB
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func isSubStructure(A *TreeNode, B *TreeNode) bool {
    return (A!= nil && B!=nil) && (helper(A,B) || isSubStructure(A.Left,B) || isSubStructure(A.Right,B))
}

func helper(A *TreeNode, B *TreeNode) bool {
    if B == nil {
        return true
    }
    if A == nil || A.Val != B.Val {
        return false
    }
    return helper(A.Left, B.Left) && helper(A.Right, B.Right)
}