题解 | #对称的二叉树#
对称的二叉树
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)
}
智元机器人成长空间 174人发布
