leetcode-solution

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 »

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 »

Shortest Path with Alternating Colors

Problem Statement Consider a directed graph, with nodes labelled 0, 1, …, n-1.  In this graph, each edge is either red or blue, and there could be self-edges or parallel edges. Each [i, j] in red_edges denotes a red directed edge from node i to node j.  Similarly, each [i, j] in blue_edges denotes a blue directed edge from node i to node j. Return an array answer of length n, where each answer[X] is the length of …

Shortest Path with Alternating Colors Read More »

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