Leetcode Golang 100. Same Tree.go

2019-04-12 11:10:18 浏览数 (1)

版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412880

思路

先判断root节点是否相等,然后递归处理左右子树

code

代码语言:javascript复制
type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func isSameTree(p *TreeNode, q *TreeNode) bool {

	if p == nil && q == nil {
		return true
	}
	if (p == nil && q != nil) || (p != nil && q == nil) {
		return false
	}
	if p.Val != q.Val {
		return false
	}
	return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
}

0 人点赞