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.

Reverse a Stack using Recursion

How to reverse a stack using Recursion? In this tutorial, I am going to discuss this interesting problem.

Given a stack of integers, we have to write a code to reverse them using recursion. For solving this problem, we don’t have to use any loop constructs (For, While etc.) or any additional data structure.

You can use stack methods like push, pop, isEmpty to solve this problem.

Reverse a stack using recursion

Odd Even Linked List

In this tutorial, I am going to discuss a programming question how to segregate even and odd nodes in a linked list or odd even linked list.

Given a singly linked list, We have to write a code to group all odd nodes together followed by the even nodes. We have to group based on node number not the value present in the node.

Try to solve this problem in O(n) time complexity and O(1) space complexity where n is the no. of nodes in a linked list.

Also, the relative order inside both the even and odd groups should remain as it was in the input. The first node is considered odd, the second node even and so on …

For example –

In the this example, Given input linked list is

1 -> 2 -> 3 -> 4 -> NULL

When we segregate even and odd nodes in a Linked List then the output is

1 -> 3 -> 2 -> 4 -> NULL