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.

Invert Binary Tree

How to Invert Binary Tree or How to convert a binary tree into its mirror tree.

Given a binary tree, we have to write a code to invert it.

Inverting a Binary Tree

Inverting a binary tree means we have to interchange the left and right children of all non-leaf nodes. In simple words, Output is the mirror of the input tree.

Cousins In Binary Tree

How to check if two nodes are cousins in a Binary tree.

Given two values x and y, we have to write a code to check the nodes corresponding to values x and y are cousins or not. All the values in this binary tree are unique.

Two nodes of a binary tree are cousins if they have the same depth, but have different parents.

In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.