Interview Question

Reverse single linked list in java using non recursive algorithm (examples)

Given the linked list, we need to reverse the linked list. The linked list is shown in Fig 1, the head is location at node 1. Each node in a list have reference to next node in the list. The node 1 will have reference to node 2 and node 2 will have reference to node 3 and so on. The linked list is null terminated or marker for end of linked list.

Check leaf nodes of binary tree are at same level – BFS / example

What is level in binary tree?
Level is distance or hops of node with respect to root node. The root node is considered to be at level 0, so children of root node will be at level 1 (level of root node + 1). So, we can calculate the level of all the nodes in binary tree.
As an example, consider the tree:

Find ceil value of input data in binary search tree in java (DFS/Example)

Given the binary search tree, we need to find the Ceil value of given input data.
What is Ceil value ? The smallest value just greater than or equal to given input data. We have already discussed about Floor of input data in BST. The logical flow of ceil value in BST is exactly same as that floor value. Let us find Ceil value for BST shown in Fig 1.

Connect nodes at same level (set next sibling)-binary tree DFS/example

Given a binary tree, we need to connect the nodes lying at same level. We have already discussed about the tree traversal using breadth first search algorithm, where we are printing the nodes level by level. By the end of this article we will achieve the same result using next sibling reference. In binary tree, we generally have following information.

Check binary tree is Quasi-Isomorphic of other tree in java (DFS / Example)

We are given two binary tree, we need to find out whether one binary tree is Quasi Isomorphic of other binary tree. Quasi Isomorphic tree? Trees are considered to Quasi isomorphic where we concerned about the structures. The structure of both tree either should be identical or obtained by swapping (or Mirror) its children.

Find level of node in binary tree – BFS (non-recursive) & example

What is level in binary tree?
Level is distance or hops of node with respect to root node. The root node is considered to be at level 0, so children of root node will be at level 1 (level of root node + 1). So, we can calculate the level of all the nodes in binary tree.
As an example, consider the tree:

Find maximum width of binary tree using BFS (with example)

Given the binary tree, We need to find the maximum width of binary tree. What is width of binary tree? The number of nodes at any level provides the width of binary tree (at that level). We will find the width at each level and maximum among these widths will be our desired output.

Find maximum sum, root to leaf path in binary tree (Java/ recursive/ example)

Given the binary tree, There are multiple paths (equal to number of leaf nodes) exist from root to leaf nodes, We need to calculate the sum of all nodes in each root to leaf path, then we need to print the maximum sum and the corresponding path. Let us take the example to elaborate our problem statement.

Number of nodes or size of binary tree in java (DFS / examples)

Given the binary tree, we need to find number of nodes in the given binary tree. To count the number of nodes in binary tree, we need to visit all the nodes at least once. We have counted the number of nodes without recursion using level order traversal in this post.

Spiral or Zigzag binary tree traversal in java (BFS & example)

Breadth First Traversal or Level Order Traversal: As name suggests, in tree traversal, where in we are traversing the breadth of tree. In this traversal, we traverse all nodes across the breadth of binary tree. In level order traversal we perform following procedure:
Traverse the binary tree level by level
At each level we go left to right

Scroll to Top