Create linked lists of all the nodes at each depth for a Binary Tree

Problem Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (i.e., if you have a tree with depth D, you’ll have D linked lists) Examples Example 1: Input: 1 / \ 2 3 / \ / \ 4 5 6 7 Output: (1) => NULL (2) => (3) => NULL (4) => (5) => (6) => (7) => NULL ...

Populate next pointer to right node in Perfect Binary Tree

Problem You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition: struct Node { int val; Node *left; Node *right; Node *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. ...

Stone Game Problem

Problem Alice and Bob play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i]. The objective of the game is to end with the most stones. The total number of stones across all the piles is odd, so there are no ties. Alice and Bob take turns, with Alice starting first. Each turn, a player takes the entire pile of stones either from the beginning or from the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins. ...

Stone Game 2 Problem

Problem Alice and Bob continue their games with piles of stones. There are a number of piles arranged in a row, and each pile has a positive integer number of stones piles[i]. The objective of the game is to end with the most stones. Alice and Bob take turns, with Alice starting first. Initially, M = 1. On each player’s turn, that player can take all the stones in the first X remaining piles, where 1 <= X <= 2M. Then, we set M = max(M, X). ...

Generate nth ugly number

Problem Given an integer n, return the nth ugly number. Ugly Number Definition Examples Example 1: Input: n = 10 Output: 12 Explanation: [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers. Example 2: Input: n = 1 Output: 1 Explanation: 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5. ...

Palindrome Partitioning Problem

Problem Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. Definition Palindrome Definition Examples Example 1: Input: s = "aab" Output: [ ["a","a","b"],["aa","b"] ] Example 2: Input: s = "a" Output: [ ["a"] ] ...

Boats to Save People Problem

Problem You are given an array people where people[i] is the weight of the ith person, and an infinite number of boats where each boat can carry a maximum weight of limit. Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person. Examples Example 1: ...

Find Duplicate Number in array containing n+1 numbers between 1 and n

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 uses only constant extra space. OR You are given an array of length n + 1 whose elements belong to the set {1, 2, ..., n}. By the pigeonhole principle, there must be a duplicate. Find it in linear time and space. ...

Binary Tree Level Order Traversal - Level by Level

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). NOTE : This problem is very similar to - Create linked lists of all the nodes at each depth for a Binary Tree Examples Example 1: 3 / \ 9 20 / \ 15 7 ...

Reverse Words in a String

Problem Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a string of the words in reverse order concatenated by a single space. Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces. ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy