Category Archives: Trees

Find a number in minimum steps

Given an infinite number line from -INFINITY to +INFINITY and we are on zero. We can move n steps either side at each n’th time. 1st time; we can move only 1 step to both ways, means -1 1; 2nd time we can move 2 steps from -1 and 1; -1 : -3 (-1-2) 1(-1+2)… Read More »

Evaluation of Expression Tree

Given a simple expression tree, consisting of basic binary operators i.e., + , – ,* and / and some integers, evaluate the expression tree. Examples: Input : Root node of the below tree Output : 100 Input : Root node of the below tree Output : 110 As all the operators in the tree are… Read More »

Print cousins of a given node in Binary Tree

Given a binary tree and a node, print all cousins of given node. Note that siblings should not be printed. Example: Input : root of below tree 1 / \ 2 3 / \ / \ 4 5 6 7 and pointer to a node say 5. Output : 6, 7 We strongly recommend you… Read More »

Iterative function to check if two trees are identical

Two trees are identical when they have same data and arrangement of data is also same. To identify if two trees are identical, we need to traverse both trees simultaneously, and while traversing we need to compare data and children of the trees. Examples: Input : Roots of below trees 10 10 / \ /… Read More »

Check if two trees are Mirror

Given two Binary Trees, write a function that returns true if two trees are mirror of each other, else false. For example, the function should return true for following input trees. We strongly recommend that you click here and practice it, before moving on to the solution. This problem is different from the problem discussed… Read More »

Create a Doubly Linked List from a Ternary Tree

Given a ternary tree, create a doubly linked list out of it. A ternary tree is just like binary tree but instead of having two nodes, it has three nodes i.e. left, middle, right. The doubly linked list should holds following properties – Left pointer of ternary tree should act as prev pointer of doubly… Read More »