1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| func isSymmetric(root *TreeNode) bool { var compare func(left, right *TreeNode) bool compare = func(left, right *TreeNode) bool { if left == nil && right == nil { return true }
if left != nil && right == nil { return false }
if left == nil && right != nil { return false }
if left.Val != right.Val { return false }
outside := compare(left.Left, right.Right) inside := compare(left.Right, right.Left) return outside && inside }
return compare(root.Left, root.Right) }
|