LeetCode - 150 - Balanced Binary Tree

The problem Given a binary tree, determine if it is height-balanced. A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one. Examples Input: root = [3,9,20,null,null,15,7] Output: true Input: root = [1,2,2,3,3,null,null,4,4] Output: false Input: root = [] Output: true Constraints The number of nodes in the tree is in the range [0, 5000]. -10^4 <= Node.val <= 10^4 Explanation From the description of the problem, we learn that we need to find if the tree is height-balanced, and that we can do it by determining if the difference between the height of every single node in the left and right subtrees is no more than 1. ...

July 30, 2025 · 4 min · Dmytro Chumakov

LeetCode - 150 - Diameter of Binary Tree

The problem Given the root of a binary tree, return the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. The length of a path between two nodes is represented by the number of edges between them. Examples ...

July 26, 2025 · 3 min · Dmytro Chumakov

LeetCode - 150 - LRU Cache

The problem Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. Implement the LRUCache class: LRUCache(int capacity) Initialize the LRU cache with positive size capacity. int get(int key) Return the value of the key if the key exists, otherwise return -1. void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key. The functions get and put must each run in O(1) average time complexity. ...

July 22, 2025 · 5 min · Dmytro Chumakov

LeetCode - 150 - Find the Duplicate Number

The problem Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. There is only one repeated number in nums, return this repeated number. You must solve the problem without modifying the array nums and using only constant extra space. Examples Input: nums = [1,3,4,2,2] Output: 2 Input: nums = [3,1,3,4,2] Output: 3 Input: nums = [3,3,3,3,3] Output: 3 Constraints 1 <= n <= 10^5 nums.length == n + 1 1 <= nums[i] <= n All the integers in nums appear only once except for precisely one integer which appears two or more times. Follow up: ...

July 17, 2025 · 4 min · Dmytro Chumakov

LeetCode - 150 - Add Two Numbers

The problem You are given two non‑empty linked lists representing two non‑negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Examples Input: l1 = [2,4,3], l2 = [5,6,4] Output: [7,0,8] Explanation: 342 + 465 = 807. Input: l1 = [0], l2 = [0] Output: [0] Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] Output: [8,9,9,9,0,0,0,1] Constraints The number of nodes in each linked list is in the range [1, 100]. 0 ≤ Node.val ≤ 9 It is guaranteed that the list represents a number that does not have leading zeros. Explanation When we add two numbers, we need to remember the carry value when it’s necessary. The main caveat in this problem is the edge cases; we will discuss them later. From the description of the problem, we know that we are given two non‑empty linked lists without negative integers, and the digits are stored in reverse order; we will see later that this reverse order will help us solve the problem. ...

July 14, 2025 · 3 min · Dmytro Chumakov