题解 | #对称的二叉树#
对称的二叉树
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) }