Tree - Data Structure

In this section we will get in depth knowledge of Tree Data Structure, including Tree interview questions with solution and Tree based logical problems.

We will also look into time and space complexity of different algorithms and problems based on Trees.

These Tree based data structures and algorithms questions with complete explanation and implementation in Java are very helpful in cracking any software engineering interview.

Example blog post alt

Iterative Traversal of Binary Tree (Inorder, Preorder and Postorder) using Stack

In an Inorder traversal if the current node is having left node, than traverse left node first, print current node value second and if current node is having right node traverse right node in the last.

Continue reading

Example blog post alt

Tree Traversals (Inorder, Preorder and Postorder) using recursion

In an Inorder traversal if the current node is having left node, than traverse left node first, print current node value second and if current node is having right node traverse right node in the last.

Continue reading

Example blog post alt

Find height of a binary tree using recursion

In this article we will find the height of a binary tree using recursion, the height of the binary tree is the number of nodes in the longest chain from root to the leaf, including both root and the leaf node.

Continue reading

Example blog post alt

Level order or breadth first tree traversal using Queue

In this article we will see "breadth first traversal" or "level order traversal" of a binary tree using stack, the level order traversal of above shown tree will be: 2, 7, 5, 2, 6, 9, 5, 11, 4

Continue reading

Example blog post alt

Level order or breadth first traversal of a binary tree using recursion

In the article we will see how to do breadth first traversal of a binary tree without Queue, The idea is to obtain height of the tree, then print the tree one level at a time. We will use two methods for this purpose one method levelOrderTraversal() will print one level at a time from 1 to height of the tree while other method printALevel() will print a specific level.

Continue reading

Example blog post alt

Reverse level order traversal of a binary tree using Recursion

To print level order traversal in reverse order we need to make sure that elements are printed only when the given level is equals to the highest level, as shown in the example code below.

Continue reading

Example blog post alt

Reverse level order traversal of a binary tree without Recursion

In this article we will see how to print reverse level order traversal of a binary tree using stack, The idea is to insert elements in a stack using simple level order traversal and than print the content of the stack to print the elements in reverse, we can do this with the help of a Queue, just need to insert right of current node first then left in the queue.

Continue reading

Got a technical query, or stuck somewhere ?