Leetcode Golang 101. Symmetric Tree.go

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

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

思路

判断一颗树是否左右对称

把一棵树当成两颗来处理

code

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

func isSymmetric(root *TreeNode) bool {
	return isMirror(root, root)
}

func isMirror(t1 *TreeNode, t2 *TreeNode) bool {
	if t1 == nil && t2 == nil {
		return true
	}
	if t1 == nil || t2 == nil {
		return false
	}

	return t1.Val == t2.Val && isMirror(t1.Right, t2.Left) && isMirror(t2.Left, t1.Right)
}

0 人点赞