Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions maximum-depth-of-binary-tree/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
func maxDepth(_ root: TreeNode?) -> Int {
if root == nil {
return 0
}

var level = 0
var stack: [(TreeNode?, Int)] = [(root, 1)]

while !stack.isEmpty {
let (current, count) = stack.removeLast()
if current?.left == nil && current?.right == nil {
level = max(level, count)
}
if let right = current?.right {
stack.append((right, count + 1))
}
if let left = current?.left {
stack.append((left, count + 1))
}
}

return level
}
}

26 changes: 26 additions & 0 deletions merge-two-sorted-lists/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class ListNode {
public var val: Int
public var next: ListNode?
public init() { self.val = 0; self.next = nil; }
public init(_ val: Int) { self.val = val; self.next = nil; }
public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
}

class Solution {
func mergeTwoLists(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? {
guard let list1 = list1 else {
return list2
}
guard let list2 = list2 else {
return list1
}

if list1.val < list2.val {
list1.next = mergeTwoLists(list1.next, list2)
} else {
list2.next = mergeTwoLists(list2.next, list1)
}
return list1.val < list2.val ? list1 : list2
}
}