Skip to content

Commit c0e09ca

Browse files
authored
Merge pull request #216 from Desgard/master
Add solution for Merge Two Binary Trees
2 parents 996fa3f + 0c5314f commit c0e09ca

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* [Microsoft](#microsoft)
2828

2929
## Progress
30-
[Problem Status](#problem-status) shows the latest progress to all 500+ questions. Currently we have 219 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
30+
[Problem Status](#problem-status) shows the latest progress to all 500+ questions. Currently we have 221 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
3131

3232

3333
## Array
@@ -154,6 +154,7 @@
154154
[Path Sum III](https://leetcode.com/problems/path-sum-iiI/)| [Swift](./Tree/PathSumIII.swift)| Easy| O(n^2)| O(1)|
155155
[Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/)| [Swift](./Tree/UniqueBinarySearchTrees.swift)| Medium| O(n^2)| O(n)|
156156
[Recover Binary Search Tree](https://leetcode.com/problems/recover-binary-search-tree/)| [Swift](./Tree/RecoverBinarySearchTree.swift)| Hard| O(n)| O(1)|
157+
[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/description/) | [Swift](./Tree/MergeTwoBinaryTrees.swift) | Easy | O(n) | O(n) |
157158

158159
## Dynamic programming
159160
| Title | Solution | Difficulty | Time | Space |
@@ -275,6 +276,7 @@
275276
[Meeting Rooms II](https://leetcode.com/problems/meeting-rooms-ii/)| [Swift](./Sort/MeetingRoomsII.swift)| Medium| O(nlogn)| O(n)|
276277
[Merge Intervals](https://leetcode.com/problems/merge-intervals/)| [Swift](./Sort/MergeIntervals.swift)| Hard| O(nlogn)| O(n)|
277278
[Alien Dictionary](https://leetcode.com/problems/alien-dictionary/)| [Swift](./Sort/AlienDictionary.swift)| Hard| O(nm)| O(nm)|
279+
[Array Partition I](https://leetcode.com/problems/array-partition-i/description/)| [Swift](./Sort/ArrayPartitionI.swift)|Easy| O(nlogn)| O(n)|
278280

279281
## Union Find
280282
| Title | Solution | Difficulty | Time | Space |

Sort/ArrayPartitionI.swift

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class ArrayPartitionI {
2+
func arrayPairSum(_ nums: [Int]) -> Int {
3+
var arr = nums
4+
arr = arr.sorted()
5+
var res = 0
6+
for i in 0 ..< arr.count {
7+
if i & 1 != 0 {
8+
res += min(arr[i], arr[i - 1])
9+
}
10+
}
11+
return res
12+
}
13+
}
14+
15+
class ArrayPartitionI_2 {
16+
func arrayPairSum(_ nums: [Int]) -> Int {
17+
return nums.sorted(by: <).enumerated()
18+
.flatMap { $0 % 2 == 0 ? $1 : nil }
19+
.reduce(0) { $0 + $1 }
20+
}
21+
}

Tree/MergeTwoBinaryTrees.swift

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public var val: Int
5+
* public var left: TreeNode?
6+
* public var right: TreeNode?
7+
* public init(_ val: Int) {
8+
* self.val = val
9+
* self.left = nil
10+
* self.right = nil
11+
* }
12+
* }
13+
*/
14+
class MergeTwoBinaryTrees {
15+
func mergeTrees(_ t1: TreeNode?, _ t2: TreeNode?) -> TreeNode? {
16+
guard let t1 = t1 else {
17+
return t2
18+
}
19+
guard let t2 = t2 else {
20+
return t1
21+
}
22+
t1.val += t2.val
23+
t1.left = mergeTrees(t1.left, t2.left)
24+
t1.right = mergeTrees(t1.right, t2.right)
25+
return t1
26+
}
27+
}

0 commit comments

Comments
 (0)