题解 | #对称的二叉树#

对称的二叉树

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

golang的队列实现

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
    }
    //双队列
    q1:=[]*TreeNode{pRoot.Left}
    q2:=[]*TreeNode{pRoot.Right}

    for len(q1)!=0&&len(q2)!=0{
        //出队
        n1:=q1[0]
        n2:=q2[0]
        q1=q1[1:]
        q2=q2[1:]
        //判断
        if n1==nil&&n2==nil{
            continue
        }else if n1!=nil&&n2!=nil{
            if n1.Val!=n2.Val{
                return false
            }else{
                q1 = append(q1, n1.Left)
                q1 = append(q1, n1.Right)

                q2 = append(q2, n2.Right)
                q2 = append(q2, n2.Left)
            }
        }else{
            return false
        }

    }
    return true
}

全部评论

相关推荐

马里奥天生励志:估计是寄了,又是一个测试发现网的
点赞 评论 收藏
分享
07-20 11:20
门头沟学院 Java
Alan_01:看到都是黑马点评跟苍穹外卖我就放心了
无实习如何秋招上岸
点赞 评论 收藏
分享
今年hc多吗。。双非真无力了
投递比亚迪等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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