From 62d1d8d7952b8ca659e1af18e5b1451542da7fa0 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Wed, 24 Apr 2024 08:56:24 +0100 Subject: [PATCH 1/2] Swift Implementation for LCCI 04.03 --- lcci/04.03.List of Depth/README.md | 56 +++++++++++++++++++++++++ lcci/04.03.List of Depth/README_EN.md | 56 +++++++++++++++++++++++++ lcci/04.03.List of Depth/Solution.swift | 53 +++++++++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 lcci/04.03.List of Depth/Solution.swift diff --git a/lcci/04.03.List of Depth/README.md b/lcci/04.03.List of Depth/README.md index 4f376f7eeedc7..a0e1ecb1cd6fd 100644 --- a/lcci/04.03.List of Depth/README.md +++ b/lcci/04.03.List of Depth/README.md @@ -311,6 +311,62 @@ impl Solution { } ``` +```swift +/* class TreeNode { +* var val: Int +* var left: TreeNode? +* var right: TreeNode? +* +* init(_ val: Int) { +* self.val = val +* self.left = nil +* self.right = nil +* } +* } +*/ + +/* class ListNode { +* var val: Int +* var next: ListNode? +* +* init(_ val: Int) { +* self.val = val +* self.next = nil +* } +* } +*/ + +class Solution { + func listOfDepth(_ tree: TreeNode?) -> [ListNode?] { + var ans = [ListNode?]() + guard let tree = tree else { return ans } + + var q = [TreeNode]() + q.append(tree) + + while !q.isEmpty { + let dummy = ListNode(0) + var cur = dummy + for _ in 0.. diff --git a/lcci/04.03.List of Depth/README_EN.md b/lcci/04.03.List of Depth/README_EN.md index 7fa66164916c2..12902b75160a2 100644 --- a/lcci/04.03.List of Depth/README_EN.md +++ b/lcci/04.03.List of Depth/README_EN.md @@ -323,6 +323,62 @@ impl Solution { } ``` +```swift +/* class TreeNode { +* var val: Int +* var left: TreeNode? +* var right: TreeNode? +* +* init(_ val: Int) { +* self.val = val +* self.left = nil +* self.right = nil +* } +* } +*/ + +/* class ListNode { +* var val: Int +* var next: ListNode? +* +* init(_ val: Int) { +* self.val = val +* self.next = nil +* } +* } +*/ + +class Solution { + func listOfDepth(_ tree: TreeNode?) -> [ListNode?] { + var ans = [ListNode?]() + guard let tree = tree else { return ans } + + var q = [TreeNode]() + q.append(tree) + + while !q.isEmpty { + let dummy = ListNode(0) + var cur = dummy + for _ in 0.. diff --git a/lcci/04.03.List of Depth/Solution.swift b/lcci/04.03.List of Depth/Solution.swift new file mode 100644 index 0000000000000..0677b63a32b71 --- /dev/null +++ b/lcci/04.03.List of Depth/Solution.swift @@ -0,0 +1,53 @@ +/* class TreeNode { +* var val: Int +* var left: TreeNode? +* var right: TreeNode? +* +* init(_ val: Int) { +* self.val = val +* self.left = nil +* self.right = nil +* } +* } +*/ + +/* class ListNode { +* var val: Int +* var next: ListNode? +* +* init(_ val: Int) { +* self.val = val +* self.next = nil +* } +* } +*/ + +class Solution { + func listOfDepth(_ tree: TreeNode?) -> [ListNode?] { + var ans = [ListNode?]() + guard let tree = tree else { return ans } + + var q = [TreeNode]() + q.append(tree) + + while !q.isEmpty { + let dummy = ListNode(0) + var cur = dummy + for _ in 0.. Date: Wed, 24 Apr 2024 08:05:47 +0000 Subject: [PATCH 2/2] style: format code and docs with prettier --- lcci/04.03.List of Depth/README.md | 14 +++++++------- lcci/04.03.List of Depth/README_EN.md | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lcci/04.03.List of Depth/README.md b/lcci/04.03.List of Depth/README.md index a0e1ecb1cd6fd..2e07ebf490e89 100644 --- a/lcci/04.03.List of Depth/README.md +++ b/lcci/04.03.List of Depth/README.md @@ -316,7 +316,7 @@ impl Solution { * var val: Int * var left: TreeNode? * var right: TreeNode? -* +* * init(_ val: Int) { * self.val = val * self.left = nil @@ -328,22 +328,22 @@ impl Solution { /* class ListNode { * var val: Int * var next: ListNode? -* +* * init(_ val: Int) { * self.val = val * self.next = nil * } -* } +* } */ class Solution { func listOfDepth(_ tree: TreeNode?) -> [ListNode?] { var ans = [ListNode?]() guard let tree = tree else { return ans } - + var q = [TreeNode]() q.append(tree) - + while !q.isEmpty { let dummy = ListNode(0) var cur = dummy @@ -351,7 +351,7 @@ class Solution { let node = q.removeFirst() cur.next = ListNode(node.val) cur = cur.next! - + if let left = node.left { q.append(left) } @@ -361,7 +361,7 @@ class Solution { } ans.append(dummy.next) } - + return ans } } diff --git a/lcci/04.03.List of Depth/README_EN.md b/lcci/04.03.List of Depth/README_EN.md index 12902b75160a2..fab4a73a2886d 100644 --- a/lcci/04.03.List of Depth/README_EN.md +++ b/lcci/04.03.List of Depth/README_EN.md @@ -328,7 +328,7 @@ impl Solution { * var val: Int * var left: TreeNode? * var right: TreeNode? -* +* * init(_ val: Int) { * self.val = val * self.left = nil @@ -340,22 +340,22 @@ impl Solution { /* class ListNode { * var val: Int * var next: ListNode? -* +* * init(_ val: Int) { * self.val = val * self.next = nil * } -* } +* } */ class Solution { func listOfDepth(_ tree: TreeNode?) -> [ListNode?] { var ans = [ListNode?]() guard let tree = tree else { return ans } - + var q = [TreeNode]() q.append(tree) - + while !q.isEmpty { let dummy = ListNode(0) var cur = dummy @@ -363,7 +363,7 @@ class Solution { let node = q.removeFirst() cur.next = ListNode(node.val) cur = cur.next! - + if let left = node.left { q.append(left) } @@ -373,7 +373,7 @@ class Solution { } ans.append(dummy.next) } - + return ans } }