Populate Next Siblings Pointers in a Given Binary Tree OR Populate Next Right Pointers in Each Node

Objective: – Given a binary tree with three pointers left, right and nextSibling). Write the program to provide the nextsibling pointers.

This problem can also be referred as “Populating Next Right Pointers in Each Node

Example:

Provide the Next Siblings Pointers in a Given Binary Tree.
Provide the Next Siblings Pointers in a Given Binary Tree.

Approach:

Read more

Diameter Of a Binary Tree

Objective: – Given a binary tree, write an algorithm to find the diameter of the tree.

What is Diameter Of a Tree: Diameter of tree is defined as A longest path or route between any two nodes in a tree. The path may or may not for through the root.

Example:

 

Diameter Of a Binary Tree

Approach:

Read more

Binary Min-Max Heap Implementation

A binary heap is a heap data structure created using a binary tree. binary tree has two rules – Binary Heap has to be a complete binary tree at all levels except the last level. This is called a shape property. All nodes are either greater than equal to (Max-Heap) or less than equal to … Read more

Colorful Numbers

Objective: Given a number, find out whether its colorful or not. Colorful Number: When in a given number, product of every digit of a sub-sequence are different. That number is called Colorful Number. See Example Example: Given Number : 3245 Output : Colorful Number 3245 can be broken into parts like 3 2 4 5 … Read more

Print All the Subsets of a Given Set (Power Set)

Objective: Given a set of numbers, print all the posssible subsets of it including empty set. Power Set: In mathematics, PowerSet of any given set S, PS(S) is set of all subsets of S including empty set. Example: S ={1,2,3} PS(S): {{ᵩ}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}. Approach: … Read more

Print All Combinations of subset of size K from Given Array

Objective: Given an array of integers of size N, print all the subsets of size k. (k<=N) Example: Generate all subsets of a fixed size k of a given set [1,2,3…n]. e.g, if n=5 and k=3, the output will look like 1 2 3 1 2 4 1 2 5 1 3 4 1 3 … Read more

AVL Tree – Insertion

What is AVL Tree : AVL tree is widely known as self-balancing binary search tree. It is named after its creator (Georgy Adelson-Velsky and Landis’ tree). In AVL Tree, the heights of child subtrees at any node differ by at most 1. At anytime if height difference becomes greater than 1 then tree balancing is … Read more

Construct a binary tree from given Inorder and Level Order Traversal

Objective: – Given a inorder and level order traversal, construct a binary tree from that. Input: Inorder and level order traversal Approach: int[] inOrder = { 4, 2, 5, 1, 6, 3, 7 }; int[] levelOrder = { 1, 2, 3, 4, 5, 6, 7 }; First element in the levelorder [] will be the … Read more

Construct Binary Search Tree from a given Preorder Traversal Using Stack (Without Recursion)

Objective: – Given a preorder traversal, construct BST from that, without using recursion. Input: Preorder traversal Similar Problem : This problem is similar to the Construct Binary Search Tree from a given Preorder Traversal using Recursion. Approach: Example: int[] preOrder = { 20, 10, 5, 1, 7, 15, 30, 25, 35, 32, 40 }; Use … Read more

Construct Binary Search Tree from a given Preorder Traversal using Recursion

Objective: – Given a preorder traversal, construct BST from that. Input: Preorder traversal Similar Problem : This problem is similar to the – Construct Binary Search Tree from a given Preorder Traversal Using Stack (Without Recursion) Approach: Solution to the problem is similar to isBST Max-Min Solution. “Your root value can have any value between … Read more

Construct a Binary Tree from Given Inorder and Depth-First-Search.

Objective: Given a inorder and pre­order tra­ver­sal, con­struct a binary tree from that.

Input: Inorder traversal and Depth-First-Search.

Approach:

int[] inOrder = { 8, 4, 2, 5, 1, 6, 3, 7, 9 };

int[] DFS = { 1, 2, 4, 8, 5, 3, 6, 7, 9 };

Read more

Inorder Predecessor and Successor in Binary Search Tree

Objective: Given a Binary Search Tree, Find predecessor and Successor of a given node.

What is Predecessor and Successor :

When you do the inorder traversal of a binary tree, the neighbors of given node are called Predecessor(the node lies behind of given node) and Successor (the node lies ahead of given node).

Example:

Inorder Predecessor and Successor in Binary Search Tree

Approach:

Read more

Rearrange Positive and Negative Elements at Alternate Positions in an Array In O(1) Extra Space

Objective: Given an array arrA[] which has negative and positive elements, rearrange the array in such a manner that positive and negative elements occupy the alternate positions and if there are extra positive or negative elements are left then append it to the end.

Examples:

int[] arrA = { 1, 2, -3, -4, -5, 6, -7, -8, 9, 10, -11, -12, -13, 14 };
Output: -13 9 -3 10 -5 6 -7 2 -12 1 -11 14 -4 -8

Naive Solution: Take the extra space of O(n), do the linear scan twice and fill the positive and negative elements at alternate positions.

But the problem becomes interesting when you have asked not to use any extra space, ask to solve in O(1).

Better Solution : Time Complexity : O(n) Space Complexity: O(1)

Use Quick sort technique.

  • Take the pivot element as 0 and do the first round of Quick Sort.
  • After above step you will have all the negative elements on left and all the positive elements on the right.

Read more

Find All Elements in an Array which appears more than N/K times, N is Array Size and k is a Number.

Objective: Given an array of size of N and number k. Find all elements in an Array which appears more than N/K times.

Input: Array [] and number k.

Example:

int[] arrA = { 2, 2, 4, 4, 3, 5, 3, 4, 4, 6, 4, 3, 3, 8 };

K = 4

N/k = 14/4 = 3

Output will be [3,4] they appear 5, 4 times respectively.

Approach:

Read more