题解 | #牛群的轴对称结构#
牛群的轴对称结构
https://www.nowcoder.com/practice/a200535760fb4da3a4568c03c1563689
考察知识点:二叉树遍历、递归
解题思路:这道题解题关键在于将一颗二叉树拆分成两棵,也就是对比根节点的两棵子树是否对称,然后同时进行LR、RL的遍历顺序进行节点遍历,比较每个节点值是否相等即可
本题解使用语言:golang
package main import . "nc_tools" /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return bool布尔型 */ func isSymmetric( root *TreeNode ) bool { // write code here if root == nil { return true } return isSymmetricTree(root.Left, root.Right) } func isSymmetricTree(root1, root2 *TreeNode) bool { if root1 == nil { return root2 == nil } if root2 == nil { return root1 == nil } if root1.Val != root2.Val { return false } return isSymmetricTree(root1.Left, root2.Right) && isSymmetricTree(root1.Right, root2.Left) }