File tree 4 files changed +78
-39
lines changed
1500-1599/1535.Find the Winner of an Array Game
2300-2399/2389.Longest Subsequence With Limited Sum
4 files changed +78
-39
lines changed Original file line number Diff line number Diff line change @@ -197,6 +197,48 @@ func expandBinaryTree(root *TreeNode) *TreeNode {
197
197
}
198
198
```
199
199
200
+ #### Swift
201
+
202
+ ``` swift
203
+ /* class TreeNode {
204
+ * var val: Int
205
+ * var left: TreeNode?
206
+ * var right: TreeNode?
207
+
208
+ * init() { self.val = 0; self.left = nil; self.right = nil }
209
+ * init(_ val: Int) { self.val = val; self.left = nil; self.right = nil }
210
+ * init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
211
+ * self.val = val
212
+ * self.left = left
213
+ * self.right = right
214
+ * }
215
+ * }
216
+ */
217
+
218
+ class Solution {
219
+ func expandBinaryTree (_ root : TreeNode? ) -> TreeNode? {
220
+ return dfs (root)
221
+ }
222
+
223
+ private func dfs (_ root : TreeNode? ) -> TreeNode? {
224
+ guard let root = root else { return nil }
225
+
226
+ let leftChild = dfs (root.left )
227
+ let rightChild = dfs (root.right )
228
+
229
+ if let leftChild = leftChild {
230
+ root.left = TreeNode (-1 , leftChild, nil )
231
+ }
232
+ if let rightChild = rightChild {
233
+ root.right = TreeNode (-1 , nil , rightChild)
234
+ }
235
+
236
+ return root
237
+ }
238
+ }
239
+
240
+ ```
241
+
200
242
<!-- tabs: end -->
201
243
202
244
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ /* class TreeNode {
2
+ * var val: Int
3
+ * var left: TreeNode?
4
+ * var right: TreeNode?
5
+
6
+ * init() { self.val = 0; self.left = nil; self.right = nil }
7
+ * init(_ val: Int) { self.val = val; self.left = nil; self.right = nil }
8
+ * init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
9
+ * self.val = val
10
+ * self.left = left
11
+ * self.right = right
12
+ * }
13
+ * }
14
+ */
15
+
16
+ class Solution {
17
+ func expandBinaryTree( _ root: TreeNode ? ) -> TreeNode ? {
18
+ return dfs ( root)
19
+ }
20
+
21
+ private func dfs( _ root: TreeNode ? ) -> TreeNode ? {
22
+ guard let root = root else { return nil }
23
+
24
+ let leftChild = dfs ( root. left)
25
+ let rightChild = dfs ( root. right)
26
+
27
+ if let leftChild = leftChild {
28
+ root. left = TreeNode ( - 1 , leftChild, nil )
29
+ }
30
+ if let rightChild = rightChild {
31
+ root. right = TreeNode ( - 1 , nil , rightChild)
32
+ }
33
+
34
+ return root
35
+ }
36
+ }
Load Diff This file was deleted.
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments