题解 | #对称的二叉树#

对称的二叉树

https://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb

package main
// import "fmt"
import . "nc_tools"
/*
 * type TreeNode struct {
 *   Val int
 *   Left *TreeNode
 *   Right *TreeNode
 * }
 */

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param pRoot TreeNode类 
 * @return bool布尔型
*/
func isSymmetrical( pRoot *TreeNode ) bool {
    if pRoot == nil {
        return true
    }
    return isSame(pRoot.Left, pRoot.Right)
}

func isSame(p1, p2 *TreeNode) bool {
    // fmt.Println(p1.Val, p2.Val)
    if p1 == nil && p2 == nil {
        return true
    }
    if p1 == nil || p2 == nil {
        return false
    }
    return p1.Val == p2.Val && isSame(p1.Left, p2.Right) && isSame(p1.Right, p2.Left)
}

全部评论

相关推荐

07-02 13:50
闽江学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务