# Ankit Tiwari

## Construct binary tree from inorder and postorder traversal

Problem Statement Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. Sample Test Case Problem Solution Postorder traversal starts at the left subtree then right subtree and then goes to the root. Inorder traversal starts at the left subtree, then goes …

## Construct tree from in-order and pre-order traversal

Problem Statement Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. Sample Test Case Problem Solution Preorder traversal starts at the root, goes to the left subtree and then to the right subtree. Inorder traversal starts at the left subtree, then …

## Inorder Successor in Binary Search Tree

Problem Statement Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Sample Test Case Problem Solution A node’s inorder successor is node with least value in its right subtree i.e. its right subtree’s left most child. If right subtree of the node doesn’t exists, …

## Sort Integers by The Number of 1 Bits

Problem Statement Given an integer array arr. You have to sort the integers in the array in ascending order by the number of 1’s in their binary representation and in case of two or more integers have the same number of 1’s you have to sort them in ascending order. Return the sorted array. Sample Test Cases Problem Solution

## Maximum XOR of Two Numbers in an Array

Problem Statement Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum result of ai XOR aj, where 0 ≤ i, j < n. Sample Test Cases Problem Solution The Approach is that instead of finding the maximum XOR of two numbers in an array, we can find two numbers in an …

## Minimum Area Rectangle

Problem Statement Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes. If there isn’t any rectangle, return 0. Sample Test Case Problem Solution Our Approach will be to sort the given points on the basis of …

## Prison Cells After N Days

Problem Statement There are 8 prison cells in a row, and each cell is either occupied or vacant. Each day, whether the cell is occupied or vacant changes according to the following rules: If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied. Otherwise, it becomes vacant. …

## Shortest Distance to Target Color

Problem Statement You are given an array colors, in which there are three colors: 1, 2 and 3. You are also given some queries. Each query consists of two integers i and c, return the shortest distance between the given index i and the target color c. If there is no solution return -1. Sample Test Cases Problem Solution The difficulty of this question is, how do we …

## Binary Search Tree to Greater Sum Tree

Problem Statement Given the root of a binary search tree with distinct values, modify it so that every node has a new value equal to the sum of the values of the original tree that are greater than or equal to node.val. As a reminder, a binary search tree is a tree that satisfies these constraints: The left subtree of a node …

## Reverse Nodes in k-Group

Problem Statement Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as …

Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]