PROBLEM DESCRIPTION Given a binary tree, return a 2-D array with vertical order traversal of it. SOLUTION public class Solution { public ArrayList<ArrayList<Integer>> verti...
Binary Tree - Level Order Traversal II
PROBLEM DESCRIPTION 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). Leetcode SOLUTION For the main explainatio...
Size of a Binary Search Tree
PROBLEM DESCRIPTION Given a binary tree, find its size (Total number of Nodes in the tree). SOLUTION class Tree { public static int getSize(Node root) { if(root == null) return 0...
Height of a Binary Search Tree
PROBLEM DESCRIPTION Given a binary tree, find its height. SOLUTION Here we are considering the height of leaf node to be 0. To handle the edge case, it is important to return -1 when the node is...
Count Days Spent Together
PROBLEM DESCRIPTION Alice and Bob are traveling to Rome for separate business meetings. You are given 4 strings arriveAlice, leaveAlice, arriveBob, and leaveBob. Alice will be in the city from th...
Evaluate Expression
PROBLEM DESCRIPTION An arithmetic expression is given by a character array A of size N. Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. E...
Double Character Trouble
PROBLEM DESCRIPTION You are given a string A. An operation on the string is defined as follows: Remove the first occurrence of the same consecutive characters. eg for a string “abbcd”, the first o...
Balanced Parenthesis
PROBLEM DESCRIPTION Given an expression string A, examine whether the pairs and the orders of “{“,”}”, ”(“,”)”, ”[“,”]” are correct in A. SOLUTION public class Solution { Map<String, Str...
Nearest Smaller Element
PROBLEM DESCRIPTION Given an array, find the nearest smaller element G[i] for every element A[i] in the array such that the element has an index smaller than i. More formally, G[i] for an elemen...
Clone Linked List with Random Pointer - Constant Space Complexity
PROBLEM DESCRIPTION A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null. Construct a deep copy of the l...