LeetCode - Blind 75 - Subtree of Another Tree

The Problem Given the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values as subRoot, and false otherwise. A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node’s descendants. The tree tree could also be considered as a subtree of itself. Examples ...

January 5, 2025 · 3 min · Dmytro Chumakov

LeetCode - Blind 75 - Same Tree

The Problem Given the roots of two binary trees p and q, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical, and the nodes have the same values. Examples Input: p = [1,2,3], q = [1,2,3] Output: true Input: p = [1,2], q = [1,null,2] Output: false ...

January 2, 2025 · 2 min · Dmytro Chumakov

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