Skip to content

Commit 8ad95f3

Browse files
committed
feat: Solution for September Challenge Day 5 - All Elements in Two Binary Search Trees
1 parent 4f03143 commit 8ad95f3

File tree

4 files changed

+211
-0
lines changed

4 files changed

+211
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*:
2+
# 1305. All Elements in Two Binary Search Trees [Medium]
3+
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/
4+
5+
---
6+
7+
### Problem Statement:
8+
9+
Given two binary search trees `root1` and `root2`.
10+
11+
Return a list containing all the integers from both trees sorted in ascending order.
12+
13+
### Example:
14+
15+
```
16+
17+
2 1
18+
/ \ / \
19+
/ \ / \
20+
1 4 0 3
21+
22+
Input: root1 = [2,1,4], root2 = [1,0,3]
23+
Output: [0,1,1,2,3,4]
24+
25+
Input: root1 = [0,-10,10], root2 = [5,1,7,0,2]
26+
Output: [-10,0,0,1,2,5,7,10]
27+
28+
Input: root1 = [], root2 = [5,1,7,0,2]
29+
Output: [0,1,2,5,7]
30+
31+
Input: root1 = [0,-10,10], root2 = []
32+
Output: [-10,0,10]
33+
34+
Input: root1 = [1,null,8], root2 = [8,1]
35+
Output: [1,1,8,8]
36+
37+
```
38+
39+
### Constraints:
40+
+ Each tree has at most 5000 nodes.
41+
+ Each node's value is between [-10^5, 10^5].
42+
43+
### Hint:
44+
+ Traverse the first tree in list1 and the second tree in list2.
45+
+ Merge the two trees in one list and sort it.
46+
47+
*/
48+
49+
50+
import UIKit
51+
52+
53+
// Definition for a binary tree node.
54+
public class TreeNode {
55+
public var val: Int
56+
public var left: TreeNode?
57+
public var right: TreeNode?
58+
public init() { self.val = 0; self.left = nil; self.right = nil; }
59+
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
60+
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
61+
self.val = val
62+
self.left = left
63+
self.right = right
64+
}
65+
}
66+
67+
// 48 / 48 test cases passed.
68+
// Status: Accepted
69+
// Runtime: 900 ms
70+
// Memory Usage: 23.7 MB
71+
72+
class Solution {
73+
func getAllElements(_ root1: TreeNode?, _ root2: TreeNode?) -> [Int] {
74+
var result = [Int]()
75+
inorder(root1, &result)
76+
inorder(root2, &result)
77+
return result.sorted()
78+
79+
}
80+
81+
private func inorder(_ root: TreeNode?,_ array: inout [Int]) {
82+
guard let root = root else {
83+
return
84+
}
85+
inorder(root.left, &array)
86+
array.append(root.val)
87+
inorder(root.right, &array)
88+
}
89+
}
90+
91+
let node1 = TreeNode(2)
92+
node1.left = TreeNode(1)
93+
node1.right = TreeNode(4)
94+
95+
let node2 = TreeNode(1)
96+
node2.left = TreeNode(0)
97+
node2.right = TreeNode(3)
98+
99+
let sol = Solution()
100+
sol.getAllElements(node1, node2)
101+
102+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*:
2+
# 05. All Elements in Two Binary Search Trees
3+
https://leetcode.com/explore/challenge/card/september-leetcoding-challenge/554/week-1-september-1st-september-7th/3449/
4+
5+
---
6+
7+
### Problem Statement:
8+
9+
Given two binary search trees `root1` and `root2`.
10+
11+
Return a list containing all the integers from both trees sorted in ascending order.
12+
13+
### Example:
14+
15+
```
16+
17+
2 1
18+
/ \ / \
19+
/ \ / \
20+
1 4 0 3
21+
22+
Input: root1 = [2,1,4], root2 = [1,0,3]
23+
Output: [0,1,1,2,3,4]
24+
25+
Input: root1 = [0,-10,10], root2 = [5,1,7,0,2]
26+
Output: [-10,0,0,1,2,5,7,10]
27+
28+
Input: root1 = [], root2 = [5,1,7,0,2]
29+
Output: [0,1,2,5,7]
30+
31+
Input: root1 = [0,-10,10], root2 = []
32+
Output: [-10,0,10]
33+
34+
Input: root1 = [1,null,8], root2 = [8,1]
35+
Output: [1,1,8,8]
36+
37+
```
38+
39+
### Constraints:
40+
+ Each tree has at most 5000 nodes.
41+
+ Each node's value is between [-10^5, 10^5].
42+
43+
### Hint:
44+
+ Traverse the first tree in list1 and the second tree in list2.
45+
+ Merge the two trees in one list and sort it.
46+
47+
*/
48+
49+
50+
import UIKit
51+
52+
53+
// Definition for a binary tree node.
54+
public class TreeNode {
55+
public var val: Int
56+
public var left: TreeNode?
57+
public var right: TreeNode?
58+
public init() { self.val = 0; self.left = nil; self.right = nil; }
59+
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
60+
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
61+
self.val = val
62+
self.left = left
63+
self.right = right
64+
}
65+
}
66+
67+
// 48 / 48 test cases passed.
68+
// Status: Accepted
69+
// Runtime: 900 ms
70+
// Memory Usage: 23.7 MB
71+
72+
class Solution {
73+
func getAllElements(_ root1: TreeNode?, _ root2: TreeNode?) -> [Int] {
74+
var result = [Int]()
75+
inorder(root1, &result)
76+
inorder(root2, &result)
77+
return result.sorted()
78+
79+
}
80+
81+
private func inorder(_ root: TreeNode?,_ array: inout [Int]) {
82+
guard let root = root else {
83+
return
84+
}
85+
inorder(root.left, &array)
86+
array.append(root.val)
87+
inorder(root.right, &array)
88+
}
89+
}
90+
91+
let node1 = TreeNode(2)
92+
node1.left = TreeNode(1)
93+
node1.right = TreeNode(4)
94+
95+
let node2 = TreeNode(1)
96+
node2.left = TreeNode(0)
97+
node2.right = TreeNode(3)
98+
99+
let sol = Solution()
100+
sol.getAllElements(node1, node2)
101+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
 (0)