LC101 - 对称二叉树
Symmetric.go |
---|
| package BinaryTree
func isSymmetric(root *TreeNode) bool {
if root == nil {
return true
}
return isImage(root.Left, root.Right)
}
func isImage(root1, root2 *TreeNode) bool {
if root1 == nil && root2 == nil {
return true
}
if root1 == nil || root2 == nil {
return false
}
return root1.Val == root2.Val && isImage(root1.Left, root2.Right) && isImage(root1.Right, root2.Left)
}
|
注意本题不能使用判断中序遍历和逆中序遍历是否相同这种方法。例如,[1,2,2,2,null,2] 这颗二叉树两种方式的遍历结果相同,但并不是对称二叉树。