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 binary tree from inorder and postorder traversal Read More »

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 …

Construct tree from in-order and pre-order traversal Read More »

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, …

Inorder Successor in Binary Search Tree Read More »

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 …

Maximum XOR of Two Numbers in an Array Read More »

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 …

Shortest Distance to Target Color Read More »

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 …

Binary Search Tree to Greater Sum Tree Read More »

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 …

Reverse Nodes in k-Group Read More »

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