github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/algorithm/leetcode/same-tree_test.go (about)

     1  package leetcode
     2  
     3  // 100. 相同的树
     4  // https://leetcode-cn.com/problems/same-tree/
     5  
     6  func isSameTree(p *TreeNode, q *TreeNode) bool {
     7  	if p == nil && q == nil { // 都是nil
     8  		return true
     9  	}
    10  	if (p == nil && q != nil) || (p != nil && q == nil) { //只有一个其中一个nil
    11  		return false
    12  	}
    13  	return q.Val == p.Val && isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
    14  
    15  }