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 Check the completeness of given binary tree | Set 2 – Using Level Order Traversal

Breadth-First Search (BFS) in 2D Matrix/2D-Array

Objective: Given a two-dimensional array or matrix, Do the breadth-First Search (BFS) to print the elements of the given matrix. Implement a Breadth-first traversal in an iterative manner. Example: int [][] grid = new int[][] { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}} Output: Breadth-First Traversal: … Read more Breadth-First Search (BFS) in 2D Matrix/2D-Array

Breadth-First Search in Disconnected Graph

Objective: Given a disconnected graph, Write a program to do the BFS, Breadth-First Search or traversal. Example: Approach: Earlier we had seen the BFS for a connected graph. In this article, we will extend the solution for the disconnected graph. Use the Queue. Create a boolean array, mark the vertex true in the array once … Read more Breadth-First Search in Disconnected Graph

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 ZigZag OR Diagonal traversal in 2d array/Matrix using queue

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 Find the nearest building which has bike | Find nearest specific vertex from source in a graph.

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 Implement Queue Using Stacks

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 morePrint The Top View of a Binary Tree