101 symmetric tree

·data-structure-and-algorithm
#binary-tree

101. 对称二叉树

go:

/**

 * Definition for a binary tree node.

 * type TreeNode struct {

 *     Val int

 *     Left *TreeNode

 *     Right *TreeNode

 * }
 */
func isSymmetric(root *TreeNode) bool {
    if root == nil {
        return true
    }

    return isMirror(root.Left, root.Right)
}

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

    if t1 == nil || t2 == nil || t1.Val != t2.Val {
        return false
    }

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