-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.swift
45 lines (37 loc) · 900 Bytes
/
code.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
39
40
41
42
43
44
import Foundation
public class TreeNode {
public var val: Int
public var left: TreeNode?
public var right: TreeNode?
public init(_ val: Int) {
self.val = val
self.left = nil
self.right = nil
}
}
func isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool {
guard let tn1 = p, let tn2 = q else {
if p == nil && q == nil {
return true
} else {
return false
}
}
if tn1.val != tn2.val {
return false
}
return isSameTree(tn1.left, tn2.left) && isSameTree(tn1.right, tn2.right)
}
let node1 = TreeNode(1)
let node2 = TreeNode(2)
let node3 = TreeNode(3)
node1.left = node2
node1.right = node3
let node11 = TreeNode(1)
let node12 = TreeNode(2)
let node21 = TreeNode(1)
let node22 = TreeNode(2)
node11.left = node12
node21.right = node22
let res = isSameTree(node11, node21)
print(res)