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. Example: Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its minimumÂ depth = … Continue reading C# Program to find Minimum Depth of Binary Tree

# Author: Mei

## Set Matrix Zeroes

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. Example 1: Input: [ [1,1,1], [1,0,1], [1,1,1] ] Output: [ [1,0,1], [0,0,0], [1,0,1] ] Example 2: Input: [ [0,1,2,0], [3,4,5,2], [1,3,1,5] ] Output: [ [0,0,0,0], [0,4,5,0], … Continue reading Set Matrix Zeroes

## Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Input: "hello" Output: "holle" Example 2: Input: "leetcode" Output: "leotcede" Solution public class Solution { public string ReverseVowels(string s) { string vowel = "aeiouAEIOU"; int left = 0, right = s.Length-1; char[] c = s.ToCharArray(); while(left … Continue reading Reverse Vowels of a String

## Sum of Left Leaves Iterative and Recursive Approach

Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. Solution DFS Approach - Recursive /** * Definition for a binary tree node. * public class … Continue reading Sum of Left Leaves Iterative and Recursive Approach

## Perform String Shifts

You are given a string s containing lowercase English letters, and a matrix shift, where shift[i] = [direction, amount]: direction can be 0 (for left shift) or 1 (for right shift). amount is the amount by which string s is to be shifted.A left shift by 1 means remove the first character of s and append it to the end.Similarly, a right shift by 1 means remove the last character … Continue reading Perform String Shifts

## Sum Root to Leaf Numbers

Hi Geeks! Welcome to 100 Days of Leetcode Programming Challenge. Day 46 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. Note: A leaf is a node with no children. Example: Input: [1,2,3] 1 … Continue reading Sum Root to Leaf Numbers

## Contiguous Array

Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. Leetcode 12th Day Challenge. Example 1: Input: [0,1] Output: 2 Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1. Example 2: Input: [0,1,0] Output: 2 Explanation: [0, 1] (or [1, … Continue reading Contiguous Array

## Convert Sorted Array to Binary Search Tree

Hi Geeks!Â Welcome to 100 Days of Leetcode Challenge. In this article, we going to see about how to Convert Sorted Array to Binary Search Tree. Day 45 of 100 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined … Continue reading Convert Sorted Array to Binary Search Tree

## Find Bottom Left Tree Value

Hi Geeks! Welcome to 100 Days of Leetcode Challenge. In this article, we going to see about how to find the bottom left tree value. Day 44 of 100 Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 3 Output: 1 Example … Continue reading Find Bottom Left Tree Value

## How to Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key.The right subtree of a node contains only nodes with keys greater than the node's key.Both the left and right subtrees must also … Continue reading How to Validate Binary Search Tree

## How to find the Height and Diameter of the Binary Tree

Hi friends, in this article, we going to see about how to find the height and diameter of Binary Tree. Height of Binary Tree Before finding the diameter of Binary Tree, we must have basic knowledge of how to find the Height of binary tree. Look at the below problem to find the Height(Maximum Depth) … Continue reading How to find the Height and Diameter of the Binary Tree

## Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a … Continue reading Binary Search Tree : Insertion

## Min Stack

Hi Geeks. Welcome to 100 Days of Leetcode challenge. Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.getMin() -- Retrieve the minimum element in the stack. Example: … Continue reading Min Stack

## Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 … Continue reading Path Sum II

## Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its zigzag level order traversal as: [ [3], [20,9], [15,7] ] Solution Level … Continue reading Binary Tree Zigzag Level Order Traversal

## Binary Tree Paths

Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example: Input: 1 / \ 2 3 \ 5 Output: ["1->2->5", "1->3"] Explanation: All root-to-leaf paths are: 1->2->5, 1->3 DFS Straight Forward Approach /** * Definition for a binary tree node. * public class TreeNode { * public int … Continue reading Binary Tree Paths

## Binary Tree Right Side View

Given 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. Example: Input: [1,2,3,null,5,null,4] Output: [1, 3, 4] Explanation: 1 <--- / \ 2 3 <--- \ \ 5 4 <--- Solution Perform Level Order traversal with simple condition /** * Definition for … Continue reading Binary Tree Right Side View

## Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] Solution - Easy to understand /** * Definition for a binary tree … Continue reading Binary Tree Level Order Traversal

## Group Anagrams

Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ] Note: All inputs will be in lowercase.The order of your output does not matter. Leetcode 6th day challenge C# Program Solution - Easy to Understand public class Solution { public IList<IList<string>> GroupAnagrams(string[] strs) { … Continue reading Group Anagrams

## Best Time to Buy and Sell Stock II

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times). Note: You may not engage in multiple transactions at the … Continue reading Best Time to Buy and Sell Stock II

## Reverse Substrings Between Each Pair of Parentheses

Introduction Hi friends! Welcome to 100 Days of Leetcode Challenge. In this article, we going to see about Reverse Substrings Between Each Pair of Parentheses. Language I took C# programming language to solve this problem. Since language is independent of the problem. You can use whatever language you wish to solve. Only problem solving approach(logic) … Continue reading Reverse Substrings Between Each Pair of Parentheses

## Find Largest Value in Each Tree Row

Hi Geeks! Welcome to 100 Days of Leetcode Challenge. In this article, you going to see about Find Largest Value in Each Tree Row. Day 30 You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 9 Output: [1, … Continue reading Find Largest Value in Each Tree Row

## Move Zeroes

Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. Example: Input: [0,1,0,3,12] Output: [1,3,12,0,0] Note: You must do this in-place without making a copy of the array.Minimize the total number of operations. Approach For simple solution, we can do by introducing one variable … Continue reading Move Zeroes

## Maximum Sub Array

Hi Geeks! Welcome to 100 Days of Leetcode Challenge. In this article, we going to see about Maximum Subarray. Day 29 Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example: Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6. Follow … Continue reading Maximum Sub Array

## Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its bottom-up level order traversal as: [ [15,7], [9,20], [3] ] Solution Iterative Approach /** * Definition … Continue reading Binary Tree Level Order Traversal II

## Happy Number

Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly … Continue reading Happy Number

## Average of Levels in Binary Tree

Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. Example 1: Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11] Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on … Continue reading Average of Levels in Binary Tree

## Invert Binary Tree

Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Solution /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; … Continue reading Invert Binary Tree

## Root To Leaf Sum Binary Tree

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 … Continue reading Root To Leaf Sum Binary Tree

## Binary Tree Preorder Traversal – Iterative way

Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,2,3] Follow up:Â Recursive solution is trivial, could you do it iteratively? Iterative Solution /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; … Continue reading Binary Tree Preorder Traversal – Iterative way

## Binary Tree Inorder Traversal

Hi Geeks! In this article, we going to look at the Binary Tree in Inorder Traversal. Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,3,2] Follow up:Â Recursive solution is trivial, could you do it iteratively? Solution - Recursive approach /** * Definition for a … Continue reading Binary Tree Inorder Traversal

## Implement an LRU Cache

In this article, I explained about LRU Cache implementation with example. Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.put(key, value) - Set or insert the value … Continue reading Implement an LRU Cache

## Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Return whether the stack is empty. Example: MyStack stack = new MyStack(); stack.push(1); stack.push(2); stack.top(); // returns 2 stack.pop(); // returns 2 stack.empty(); // … Continue reading Implement Stack using Queues

## Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.empty() -- Return whether the queue is empty. Example: MyQueue queue = new MyQueue(); queue.push(1); queue.push(2); queue.peek(); // returns 1 queue.pop(); // returns … Continue reading Implement Queue using Stacks

## First Missing Positive

Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2,0] Output: 3 Example 2: Input: [3,4,-1,1] Output: 2 Example 3: Input: [7,8,9,11,12] Output: 1 Note: Your algorithm should run inÂ O(n) time and uses constant extra space. Solution With comments public class Solution { public int firstMissingPositive(int[] nums) { int n = … Continue reading First Missing Positive

## Find All Numbers Disappeared in an Array

Given an array of integers where 1 â‰¤ a[i] â‰¤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array. Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count … Continue reading Find All Numbers Disappeared in an Array

## Find All Duplicates in an Array

Given an array of integers, 1 â‰¤ a[i] â‰¤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,8,2,3,1] Output: [2,3] Approach When find a number i, flip the number at position i-1 to negative.If … Continue reading Find All Duplicates in an Array

## How Many Numbers Are Smaller Than the Current Number

Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i]. Return the answer in an array. Example 1: Input: nums = [8,1,2,2,3] Output: [4,0,1,1,3] Explanation: For nums[0]=8 there exist four smaller numbers than … Continue reading How Many Numbers Are Smaller Than the Current Number

## Minimum Remove to Make Valid Parentheses

Introduction In this article, we will see about Minimum Remove to Make Valid Parentheses. We have to remove the minimum number of parentheses to make a string with valid parentheses. BACKGROUND Day 15 of 100 Days of Leetcode Programming Challenge. This problem tests our knowledge in the Stack data structure. LANGUAGE I took C# programming language … Continue reading Minimum Remove to Make Valid Parentheses

## Alphabet Board Path

INTRODUCTION In this article, we will see about Alphabet Board Path. It is one of the interesting problem, find Alphabet Board Path. BACKGROUND Day 14 of 100 Days of Leetcode Programming Challenge. This is one of the top Leetcode Problem. This problem tests our logical skills. LANGUAGE I took C# programming language to solve this … Continue reading Alphabet Board Path

## How to Reorganize String?

INTRODUCTION In this article, we will see about How to Reorganize String? It is one of the interesting problem, Reorganizing string. Background Day 13 of 100 Days of Leetcode Programming Challenge. This is one of the top Leetcode Problem. This problem tests our knowledge in string manipulations and hashing. Related Problems Top K Frequent ElementsHow … Continue reading How to Reorganize String?

## How to Sort Characters By Frequency?

Introduction In this article, we will see about How to sort characters based upon frequency? Background Day 12 of 100 Days of Leetcode Programming Challenge. This is one of the top Leetcode Problem. This problem tests our knowledge in Map data structure and Sorting. Language I took C# programming language to solve this problem. Since … Continue reading How to Sort Characters By Frequency?

## Maximum Depth of Binary Tree

In this article, I explain about how to find the Maximum Depth of Binary Tree which is also called as Height of Binary Tree. Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Note: A … Continue reading Maximum Depth of Binary Tree

## Look and Say (Count and Say) Sequence

Day 10 Find the nth term in Count and Say Sequence. The Count and Say sequence is the sequence of below integers: "1" "11" "21" "1211" "111221" "312211" "13112221" "1113213211" "31131211131221" "13211311123113112211" "11131221133112132113212221" "3113112221232112111312211312113211" "1321132132111213122112311311222113111221131221" "11131221131211131231121113112221121321132132211331222113112211" "311311222113111231131112132112311321322112111312211312111322212311322113212221" For example: If you have "1", the next line is "11".If you have "11", the next line is … Continue reading Look and Say (Count and Say) Sequence

## Find the Missing Number in an Array

In this article, I explained about how to find the missing number in an array. Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. Example 1: Input: [3,0,1] Output: 2 Example 2: Input: [9,6,4,2,3,5,7,0,1] Output: 8 Note:Your algorithm should run in linear run time … Continue reading Find the Missing Number in an Array

## Squares of a Sorted Array

Day 8 Welcome to 100 Days of Leetcode Challenge. Given an array of integersÂ AÂ sorted in non-decreasing order,Â return an array of the squares of each number,Â also in sorted non-decreasing order. Example 1: Input: [-4,-1,0,3,10] Output: [0,1,9,16,100] Example 2: Input: [-7,-3,2,3,11] Output: [4,9,9,49,121] Note: 1 <= A.length <= 10000-10000 <= A[i] <= 10000A is sorted in non-decreasing order. … Continue reading Squares of a Sorted Array

## How to Reverse Bits – Bit Manipulation

Reversing bits is one of the popular problem. How to reverse bits? In this article, we going to see about how to reverse bits in an efficient way. Reversing bits tests out understanding about bits. One of the top leetcode problem in bits section. Let's see our given problem, Reverse bits of a given 32 … Continue reading How to Reverse Bits – Bit Manipulation

## Rotate Array

Hi Geeks! Welcome to 100 Days of Leetcode Challenge. DAY 6 In this article, I'm going to explain about how to rotate an array efficiently. This is nothing but just playing with Array. Here is our given problem, Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: [1,2,3,4,5,6,7] and k … Continue reading Rotate Array

## Sqrt(x)

Hi Geeks! Welcome to 100 Days of Leetcode Challenge. Day 5 Today we going to see about implementation of sqrt(x). Let's jump into our given question, Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a non-negative integer. Since the return type is an integer, the decimal digits are truncated and only … Continue reading Sqrt(x)

## Evaluate Reverse Polish Notation

Hi Geeks! Welcome to 100 Days of Leetcode Challenge. Day 4 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Note: Division between two integers should truncate toward zero.The given RPN expression is always valid. That means the expression would always evaluate … Continue reading Evaluate Reverse Polish Notation