class TreeNode {
var previous: TreeNode?
var next: TreeNode?
var data: Int?
func isSymmeTrical(node: TreeNode?) -> Bool {
if node == nil || (node?.previous == nil && node?.next == nil) {
return true
}
return isSymmeTrical(left: node!.previous, right: node!.next)
}
private func isSymmeTrical(left: TreeNode?, right: TreeNode?) -> Bool {
if left == nil && right == nil {
return true
} else if left == nil || right == nil {
return false
} else if left?.data != right?.data {
return false
}
return isSymmeTrical(left: left?.previous, right: left?.next) && isSymmeTrical(left: right?.previous, right: right?.next)
}
}
网友评论