LeetCode - Blind 75 - Construct Binary Tree from Preorder and Inorder Traversal

The Problem Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree. Examples Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7] Output: [3,9,20,null,null,15,7] Input: preorder = [-1], inorder = [-1] Output: [-1] Constraints 1 <= preorder.length <= 3000 inorder.length == preorder.length -3000 <= preorder[i], inorder[i] <= 3000 preorder and inorder consist of unique values. Each value of inorder also appears in preorder. preorder is guaranteed to be the preorder traversal of the tree. inorder is guaranteed to be the inorder traversal of the tree. Depth First Search Solution func buildTree(_ preorder: [Int], _ inorder: [Int]) -> TreeNode? { if preorder.isEmpty || inorder.isEmpty { return nil } let rootVal = preorder[0] let root = TreeNode(rootVal) guard let mid = inorder.firstIndex(of: rootVal) else { return nil } if mid > 0 { root.left = buildTree(Array(preorder[1...mid]), Array(inorder[0..<mid])) } if mid < inorder.count - 1 { root.right = buildTree(Array(preorder[mid + 1..<preorder.count]), Array(inorder[mid + 1..<inorder.count])) } return root } Explanation We can solve this problem by understanding how preorder and inorder traversal work. ...

January 23, 2025 · 3 min · Dmytro Chumakov

LeetCode - Blind 75 - Kth Smallest Element in a BST

The Problem Given the root of a binary search tree and an integer k, return the kth smallest value (1-indexed) among all the values of the nodes in the tree. Examples Input: root = [3,1,4,null,2], k = 1 Output: 1 Input: root = [5,3,6,2,4,null,null,1], k = 3 Output: 3 Constraints The number of nodes in the tree is n. 1 <= k <= n <= 10⁴ 0 <= Node.val <= 10⁴ Follow-up: If the BST is modified often (i.e., we can perform insert and delete operations) and you need to find the kth smallest element frequently, how would you optimize? ...

January 20, 2025 · 2 min · Dmytro Chumakov

LeetCode - Blind 75 - Validate Binary Search Tree

The Problem Given the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node’s key. The right subtree of a node contains only nodes with keys greater than the node’s key. Both the left and right subtrees must also be binary search trees. A subtree of treeName is a tree consisting of a node in treeName and all of its descendants. ...

January 16, 2025 · 3 min · Dmytro Chumakov

LeetCode - Blind 75 - Binary Tree Level Order Traversal

The Problem Given the root of a binary tree, return the level order traversal of its nodes’ values (i.e., from left to right, level by level). Examples Input: root = [3,9,20,null,null,15,7] Output: [[3],[9,20],[15,7]] Input: root = [1] Output: [[1]] Input: root = [] Output: [] Constraints The number of nodes in the tree is in the range [0, 2000]. -1000 <= Node.val <= 1000 BFS Solution func levelOrder(_ root: TreeNode?) -> [[Int]] { if root == nil { return [] } var queue: [TreeNode?] = [] queue.append(root) var res: [[Int]] = [] while !queue.isEmpty { var level: [Int] = [] for _ in 0 ..< queue.count { let node = queue.removeFirst() if let node = node { level.append(node.val) queue.append(node.left) queue.append(node.right) } } if !level.isEmpty { res.append(level) } } return res } Explanation When you see that a binary tree problem involves level order traversal, it means the solution usually implies the breadth-first search (BFS) algorithm. BFS is a common solution to this problem because, according to Wikipedia, breadth-first search is also called “level-order search.” The core idea behind BFS is to iterate level by level from top to bottom, and from left to right. ...

January 8, 2025 · 2 min · Dmytro Chumakov

LeetCode - Blind 75 - Lowest Common Ancestor of a Binary Search Tree

The Problem Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).” Examples ...

January 7, 2025 · 3 min · Dmytro Chumakov