-
Notifications
You must be signed in to change notification settings - Fork 0
/
101SymmetricTree.swift
38 lines (34 loc) · 1.44 KB
/
101SymmetricTree.swift
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
27
28
29
30
31
32
33
34
35
36
37
38
/*
Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
Exmaple 1:
Input: root = [1,2,2,3,4,4,3]
Output: true
Visualized - https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg
Example 2:
Input: root = [1,2,2,null,3,null,3]
Output: false
Visualized - https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg
*/
struct Solution {
static func isSymmetric(_ root: TreeNode?) -> Bool {
guard let root = root else { return true }
let leftValuesArray = treeNodeValuesArray(node: root.left, mirrored: false)
let rightValuesArray = treeNodeValuesArray(node: root.right, mirrored: true)
return leftValuesArray == rightValuesArray
}
static func treeNodeValuesArray(node: TreeNode?, mirrored: Bool) -> [Int?] {
guard let node = node else { return [] }
guard node.left != nil || node.right != nil else { return [node.val] }
var mappedArray: [Int?] = [node.val]
let left = node.left == nil ? [nil] : treeNodeValuesArray(node: node.left, mirrored: mirrored)
let right = node.right == nil ? [nil] : treeNodeValuesArray(node: node.right, mirrored: mirrored)
if mirrored {
mappedArray.append(contentsOf: right)
mappedArray.append(contentsOf: left)
} else {
mappedArray.append(contentsOf: left)
mappedArray.append(contentsOf: right)
}
return mappedArray
}
}