给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/ \
2 2
/ \ / \
3 4 4 3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/ \
2 2
\ \
3 3
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/symmetric-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
假定出一个垂直倒转的新二叉树,要和原树一样
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isSymmetric(root *TreeNode) bool {
return isMirror(root, root)
}
func isMirror(t1, t2 *TreeNode) bool {
if t1 == (*TreeNode)(nil) && t2 == (*TreeNode)(nil) {
return true
} else if t1 != (*TreeNode)(nil) && t2 != (*TreeNode)(nil) && t1.Val == t2.Val {
return isMirror(t1.Left, t2.Right) && isMirror(t1.Right, t2.Left)
}
return false
}