题解 | #对称的二叉树#

对称的二叉树

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

package main

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

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param pRoot TreeNode类 
 * @return bool布尔型
*/
func isSymmetrical( pRoot *TreeNode ) bool {
    // write code here

    if pRoot == nil {
        return true
    } 
    return isSame(pRoot.Left, pRoot.Right)
   
}

func isSame(pRoot1, pRoot2 *TreeNode) bool {
    if pRoot1 == nil && pRoot2 == nil {
        return true
    }

    if pRoot1 == nil || pRoot2 == nil {
        return false
    }

    return pRoot1.Val == pRoot2.Val && isSame(pRoot1.Left, pRoot2.Right) && isSame(pRoot1.Right, pRoot2.Left)
}

全部评论

相关推荐

09-13 17:25
亲切的00后在笔试:我也遇到了,所以我早他一步查看图片
点赞 评论 收藏
分享
09-16 14:01
井冈山大学 Java
肖先生~:兄弟们,我发的她都点赞了,但是就是不给我微信
秋招被确诊为……
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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