In a rooted binary tree, we need to find the lowest common ancestor (LCA) of the deepest leaves. The deepest leaves are the leaf nodes that are furthest from the root. Key concepts: A leaf node has no children The depth of the root is 0, and each child's depth is parent's depth + 1 The LCA is the deepest node that contains all target nodes in its subtree Algorithm We use a recursive approach that returns both the depth and the LCA node: If a node is null, return depth 0 ... Read More
Given the root of a binary tree, we need to find the maximum average value of any subtree in that tree. A subtree's average is calculated by dividing the sum of all nodes in the subtree by the count of nodes. 5 6 1 Maximum average: 6.0 (node 6) For the example tree above ? Node 5 subtree: (5 + ... Read More
In an infinite binary tree where every node has two children, nodes are labeled in a zigzag pattern. Odd-numbered rows (1st, 3rd, 5th...) are labeled left to right, while even-numbered rows (2nd, 4th, 6th...) are labeled right to left. 1 3 2 4 5 6 7 ... Read More
The problem asks us to find the earliest moment when everyone in a social group becomes acquainted with everyone else. This is a classic Union-Find (Disjoint Set Union) problem where we need to track connected components and determine when all people form a single connected group. Given N people with IDs from 0 to N-1 and a list of friendship logs, each log contains [timestamp, person_A, person_B]. We need to find the earliest timestamp when all people are connected through friendships. Algorithm Overview We use the Union-Find data structure with the following approach − Sort ... Read More
A broken calculator problem involves finding the minimum operations needed to transform one number into another using only two allowed operations: double (multiply by 2) and decrement (subtract 1). Given an initial number X displayed on the calculator, we need to reach the target number Y using the minimum number of operations. Problem Understanding The calculator supports only two operations: Double − Multiply the current number by 2 Decrement − Subtract 1 from the current number For example, if X = 5 and Y = 8, we can reach 8 from 5 in ... Read More
The Insert Delete GetRandom O(1) problem requires designing a data structure that supports three operations in average O(1) time: inserting elements, removing elements, and returning a random element with equal probability. Problem Requirements insert(val) − Insert an item val to the set if not already present remove(val) − Remove an item val from the set if it exists getRandom() − Return a random element with equal probability for all elements Solution Approach We use two data structures to achieve O(1) operations: Dictionary (present) − Maps values to their indices in the array ... Read More
A nested list iterator flattens a multi-level list structure into a single sequence. Each element can be either an integer or another list containing integers or nested lists. Problem Statement Given a nested list like [[1, 1], 2, [1, 1]], we need to create an iterator that returns elements in flattened order: 1, 1, 2, 1, 1. Algorithm The solution uses recursion to flatten the nested structure during initialization ? Initialize an empty result list and index counter Recursively traverse the nested list using getVal() If element is integer, add to result list If ... Read More
The Course Schedule problem asks whether we can complete all courses given their prerequisites. This is essentially a cycle detection problem in a directed graph where courses are nodes and prerequisites are edges. Problem Understanding Given a number of courses and prerequisite pairs, we need to determine if it's possible to finish all courses. For example, if we have 2 courses and prerequisites = [[1, 0]], it means to take course 1, we must first complete course 0. This is possible, so we return True. Algorithm Approach We solve this using Depth-First Search (DFS) with cycle ... Read More
The Minimum Cost Tree From Leaf Values problem asks us to build a binary tree from an array where each leaf corresponds to array elements in order, and each internal node's value equals the product of the maximum leaf values in its left and right subtrees. We need to find the minimum sum of all internal node values. Given an array of positive integers, we must construct binary trees such that: Each node has either 0 or 2 children Array values correspond to leaf values in an inorder traversal Each non-leaf node equals the product of the ... Read More
A Valid Parentheses String (VPS) consists only of "(" and ")" characters and satisfies specific structural properties. The goal is to split a VPS into two disjoint subsequences A and B such that the maximum nesting depth is minimized. Understanding Valid Parentheses Strings A string is a Valid Parentheses String if ? It is the empty string, or It can be written as AB, where A and B are VPS's, or It can be written as (A), where A is a VPS. Nesting Depth Definition The nesting depth of a VPS is defined ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance