Sort a Stack using Recursion

In this problem, We have to write a code to sort a stack using recursion. We have to sort in a descending order (Top of the stack has the greatest element).

We don’t have to use any loop constructs ( for, while etc) or additional data structure.

For Example –

In this example, You can see after sorting the stack, the element which has greater value is at the top of the stack.

Sort a stack using recursion

Find Nth Ugly Number

Given a number N, we have to write a code to find the Nth ugly number.

What is Ugly Numbers?

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. In simple words, The number which can be represented as the product of 2, 3 and 5 are ugly numbers (2^i * 3^i * 5^i).

For Example –

Input : n = 10, Output : 12

12 is the 10th ugly number. The first 10 ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12.

Note:  

1 is typically treated as an ugly number.

n does not exceed 1690.

Check If Two Binary Trees are Identical (Same Tree)

How to Check If Two Binary Trees are Identical.

Given two binary trees, write a code to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical and the nodes have the same value.

For example: (Check the image)

Example 1: true

In this first example, Both the trees are same. They have the same structure as well as their node values are same.

Example 2: false

In the second example, Both the trees are structurally same but the value of left children of tree A is not equal to tree B.

Example 3: false

Both the trees are structurally not identical.