Check the completeness of given binary tree | Set 2 – Using Level Order Traversal

Objective: Given a binary tree, write an algorithm to determine whether the tree is complete or not using Level order traversal. Earlier we had solved this problem by counting the number of nodes in the tree. (Read – Check the completeness of given binary tree | Set 1 – Using Node Count. In this article, … Read more

ZigZag OR Diagonal traversal in 2d array/Matrix using queue

Objective: Given a two-dimensional array or matrix, Write an algorithm to print the given matrix in a zigzag manner or in other words print all the diagonals of the matrix. Example: int [][] grid = new int[][] {            {1, 2, 3, 4},            {5, 6, 7, 8},            {9, 10, 11, 12},            {13, 14, 15, 16}} Output:  … Read more

Find the nearest building which has bike | Find nearest specific vertex from source in a graph.

Objective: Given a matrix (NxN) which represents the buildings in community. You are in a building and you need a bike to travel.  There are few buildings in community which has bikes which you can take for your ride. Write an algorithm to find the nearest building which has bike and distance from your building. … Read more

Implement Queue Using Stacks

Objective: We know that Queue is FIFO (First-in-First-Out) and Stack is LIFO ( Last-in-First-Out). Here our objective is to implement queue using stacks. Approach: Take 2 Stacks, stack1 and stack2. stack1 will be used a back of the Queue and stack2 will be used as front of the Queue. Push() operation will be done on … Read more

Print The Top View of a Binary Tree

Objective: Given a binary tree, print it in Top View of it.

What is Top View: Top view means when you look the tree from the top the nodes you will see will be called the top view of the tree. See the example below.

Print The Top View of a Binary Tree.
Print The Top View of a Binary Tree.

as you can see in the example above,8, 4, 2, 1, 3, 7 is the Top view of the given binary tree.

Approach:

Read more