diff --git a/lcci/04.03.List of Depth/README.md b/lcci/04.03.List of Depth/README.md index 4f376f7eeedc7..2e07ebf490e89 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..fab4a73a2886d 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..