Largest Binary Search Tree BST Subtree

Problem Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it. Note: A subtree must include all of its descendants. Examples Example 1: 10 / \ [5] 15 / \ \ [1] [8] 7 ...

Longest Consecutive Sequence Problem

Problem Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Examples Example 1: Input: nums = [100,4,200,1,3,2] Output: 4 Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4. ...

Find the Celebrity Problem

Problem Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist one celebrity. The definition of a celebrity is that all the other n - 1 people know him/her but he/she does not know any of them. Now you want to find out who the celebrity is or verify that there is not one. The only thing you are allowed to do is to ask questions like: “Hi, A. Do you know B?” to get information of whether A knows B. You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense). ...

Bitwise AND of Numbers Range Problem

Problem Given two integers left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive. Examples Example 1: Input: left = 5, right = 7 Output: 4 Example 2: Input: left = 0, right = 0 Output: 0 ...

Knight Probability in Chessboard Problem

Problem On an n x n chessboard, a knight starts at the cell (row, column) and attempts to make exactly k moves. The rows and columns are 0-indexed, so the top-left cell is (0, 0), and the bottom-right cell is (n - 1, n - 1). A chess knight has eight possible moves it can make, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction. ...

Add Two Numbers represented as Linked List in Forward

Problem What if the digits are stored in regular order instead of reversed order? Example Example 1: Input: l1: 1 -> 0 -> 0 -> 7 l2: 9 -> 3 Output: 1 -> 1 -> 0 -> 0 Explanation: Here are 2 numbers: 1007 and 93. On addition we get 1100. ...

Course Schedule 1 - Is it Possible

Problem There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai. For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1. Return true if you can finish all courses. Otherwise, return false. ...

Course Schedule 2 - Get Ordered Courses

Problem There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai. For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1. Return the ordering of courses you should take to finish all courses. If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array. ...

All Nodes Distance K in Binary Tree Problem

Problem Given the root of a binary tree, the value of a target node target, and an integer k, return an array of the values of all nodes that have a distance k from the target node. You can return the answer in any order. Examples graph TB; 3 --- 5 & 1; 5 --- 6 & 2; 1 --- 0 & 8; 2 --- 7 & 4; style 5 fill:#FF9933 style 1 fill:#138808 style 7 fill:#138808 style 4 fill:#138808 ...

Minimum Depth of Binary Tree

Minimum Depth of Binary Tree Problem Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Note: A leaf is a node with no children. Examples Example 1: graph TD; 3 --- 9 & 20; 20 --- 15 & 7; ...

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