-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path100_SameTree.swift
46 lines (37 loc) · 1019 Bytes
/
100_SameTree.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
45
46
//
// 100_SameTree.swift
// HRSwift
//
// Created by yansong li on 2016-08-02.
// Copyright © 2016 yansong li. All rights reserved.
//
import Foundation
/**
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
*/
/**
Swift Knowledge:
Enum Comparison
Algorithem Knowledge:
Recursive call.
*/
/**
100. Same Tree
https://leetcode.com/problems/same-tree/
*/
class Solution_SameTree {
func isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool {
switch(p, q){
case (let .some(leftRoot), let .some(rightRoot)):
let valEqual = (leftRoot.val == rightRoot.val)
let leftEqual = isSameTree(leftRoot.left, rightRoot.left)
let rightEqual = isSameTree(leftRoot.right, rightRoot.right)
return valEqual && leftEqual && rightEqual
case (.none, .none):
return true
default:
return false
}
}
}