LeetCode - Blind 75 - Maximum Depth of Binary Tree

The Problem Given the root of a binary tree, return its maximum depth. A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Examples Input: root = [3,9,20,null,null,15,7] Output: 3 Input: root = [1,null,2] Output: 2 Constraints The number of nodes in the tree is in the range [0, 10⁴]. -100 <= Node.val <= 100 Brute Force Solution func maxDepth(_ root: TreeNode?) -> Int { if root == nil { return 0 } return 1 + max(maxDepth(root?.left), maxDepth(root?.right)) } Explanation We can solve this problem using recursion by calling the maxDepth function on the left and right child nodes. This approach helps us find the longest path down the tree. ...

January 1, 2025 · 2 min · Dmytro Chumakov

LeetCode - Blind 75 - Invert Binary Tree

The Problem Given the root of a binary tree, invert the tree, and return its root. Examples Input: root = [4,2,7,1,3,6,9] Output: [4,7,2,9,6,3,1] Input: root = [2,1,3] Output: [2,3,1] Input: root = [] Output: [] Constraints The number of nodes in the tree is in the range [0, 100]. -100 <= Node.val <= 100 Depth First Search Solution func invertTree(_ root: TreeNode?) -> TreeNode? { if root == nil { return nil } let tmp = root?.left root?.left = root?.right root?.right = tmp invertTree(root?.left) invertTree(root?.right) return root } Explanation When working with trees, we often consider two general ways to solve these problems: depth-first search (DFS) and breadth-first search (BFS). In this case, we use the DFS algorithm, where we swap the left subtree with the right subtree and recursively call invertTree on the left and right nodes. ...

December 30, 2024 · 2 min · Dmytro Chumakov

LeetCode - Blind 75 - Merge k Sorted Lists

The problem You are given an array of k linked lists lists, each linked list is sorted in ascending order. Merge all the linked lists into one sorted linked list and return it. Examples Input: lists = [[1,4,5],[1,3,4],[2,6]] Output: [1,1,2,3,4,4,5,6] Explanation: The linked lists are: [ 1->4->5, 1->3->4, 2->6 ] merging them into one sorted list: 1->1->2->3->4->4->5->6 Input: lists = [] Output: [] Input: lists = [[]] Output: [] Constraints k == lists.length 0 <= k <= 10^4 0 <= lists[i].length <= 500 -10^4 <= lists[i][j] <= 10^4 lists[i] is sorted in ascending order. The sum of lists[i].length will not exceed 10^4. Brute Force Solution func mergeKLists(_ lists: [ListNode?]) -> ListNode? { var nodes: [Int] = [] // Step 1 for i in 0..<lists.count { var lst = lists[i] while lst != nil { nodes.append(lst!.val) lst = lst?.next } } // Step 2 nodes.sort() // Step 3 var res = ListNode(0) var curr: ListNode? = res for node in nodes { curr?.next = ListNode(node) curr = curr?.next } return res.next } Explanation We can solve this problem by separating it into three steps: ...

December 29, 2024 · 3 min · Dmytro Chumakov

LeetCode - Blind 75 - Linked List Cycle

The Problem Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that the tail’s next pointer is connected to. Note that pos is not passed as a parameter. Return true if there is a cycle in the linked list. Otherwise, return false. ...

December 26, 2024 · 2 min · Dmytro Chumakov

LeetCode - Blind 75 - Remove Nth Node From End of List

The Problem Given the head of a linked list, remove the nth node from the end of the list and return its head. Examples Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5] Input: head = [1], n = 1 Output: [] Input: head = [1,2], n = 1 Output: [1] Constraints The number of nodes in the list is sz. 1 <= sz <= 30 0 <= Node.val <= 100 1 <= n <= sz Brute Force Solution func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { var nodes: [ListNode] = [] var curr = head while curr != nil { nodes.append(curr!) curr = curr!.next } let removeIndex = nodes.count - n if removeIndex == 0 { return head?.next } nodes[removeIndex - 1].next = nodes[removeIndex].next return head } Explanation One way to solve this problem is to use extra memory by iterating over all elements in head and storing them in an array. The brute force solution allows us to know the index of each element and delete the node. ...

December 25, 2024 · 3 min · Dmytro Chumakov