美文网首页Leetcodeleetcode
101. Symmetric Tree.go

101. Symmetric Tree.go

作者: AnakinSun | 来源:发表于2019-03-26 12:13 被阅读4次

判断树是否对称
一般涉及到树的问题,都是采用递归的处理方式

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)
}

相关文章

网友评论

    本文标题:101. Symmetric Tree.go

    本文链接:https://www.haomeiwen.com/subject/ymlgvqtx.html