LeetCode - 150 - Binary Tree Right Side View

The problem Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. Examples Input: root = [1,2,3,null,5,null,4] Output: [1,3,4] Explanation: Input: root = [1,2,3,4,null,null,null,5] Output: [1,3,4,5] Explanation: Input: root = [1,null,3] Output: [1,3] Input: root = [] Output: [] Constraints The number of nodes in the tree is in the range [0, 100]. -100 <= Node.val <= 100 Explanation Let’s imagine that we have a person on the right side and if we look at the first example, we can see everything on the right side. ...

August 5, 2025 · 4 min · Dmytro Chumakov

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