//对称二叉树

法1:递归
func isSymmetric(root *TreeNode) bool {
    return check(root, root)
}

//根据镜像的轨迹来遍历2颗树(即一个访问左子树时,另一个访问右子数),并比较值是否相等

func check(p, q *TreeNode) bool {
    if p == nil && q == nil {
        return true
    }
    if p == nil || q == nil {
        return false
    }
    return p.Val == q.Val && check(p.Left, q.Right) && check(p.Right, q.Left) 
}


法2:迭代(递归转迭代 需要用到队列)
//比较两个相邻的节点是否相等
func isSymmetric(root *TreeNode) bool {
    u, v := root, root
    //队列
    q := []*TreeNode{}
    q = append(q, u)
    q = append(q, v)

    for len(q) > 0 {
        //取出队列中两个连续的值
        u, v = q[0], q[1]
        //清空队列
        q = q[2:]
        if u == nil && v == nil {
            continue
        }
        if u == nil || v == nil {
            return false
        }
        if u.Val != v.Val {
            return false
        }
        q = append(q, u.Left)
        q = append(q, v.Right)

        q = append(q, u.Right)
        q = append(q, v.Left)
    }
    return true
}

参考链接:https://leetcode-cn.com/problems/symmetric-tree/solution/dui-cheng-er-cha-shu-by-leetcode-solution