-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path101.swift
37 lines (34 loc) · 828 Bytes
/
101.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
//
// 101.swift
// swift-leetcode
//
// Created by Q YiZhong on 2019/3/31.
// Copyright © 2019 YiZhong Qi. All rights reserved.
//
import Foundation
func isSymmetric(_ root: TreeNode?) -> Bool {
var l1: [Int] = []
var l2: [Int] = []
DFS(&l1, head: root, isLeft: true)
DFS(&l2, head: root, isLeft: false)
if l1 == l2 {
return true
} else {
return false
}
}
fileprivate func DFS(_ l: inout [Int], head: TreeNode?, isLeft: Bool) {
if head != nil {
l.append(head!.val)
} else {
l.append(Int.max)
return
}
if isLeft {
DFS(&l, head: head?.left, isLeft: isLeft)
DFS(&l, head: head?.right, isLeft: isLeft)
} else {
DFS(&l, head: head?.right, isLeft: isLeft)
DFS(&l, head: head?.left, isLeft: isLeft)
}
}