Perform InOrder traversal on a Binary Tree by without Recursion

Question: Given a binary tree root node, perform In Order traversal by using stacks. Example: Consider the image given below: InOrder Traversal (Left, Root, Right): 7 10 15 16 18 25 30 Now lets see how to perform In Order Traversal: (Left, Root, Right) In In-Order traversal, first Left node will be printed, then root […]

Perform PreOrder traversal on a Binary Tree by without Recursion

Question: Given a binary tree root node, perform PreOrder traversal by using stacks. Example: Consider the image given below: PreOrder Traversal(Root, Left, Right): 16 10 7 15 25 18 30 Now lets see how to perform Pre Order Traversal: (Root, Left, Right) In pre Order traversal, first root node will be printed, then left node […]

Display Reverse Level Order Traversal by using queue

Question: Given a binary tree root node, display all the nodes in reverse level order traversal. Example: Consider the image given below: Form the image above, reverse level order traversal is: 7, 15, 18, 30, 10, 25, 16 Solution: Solution is similar to level order traversal, with one difference. Here we should use Stack and […]

Display nodes at given level in Binary Tree

Question: Given a binary tree root node and a level, display all the nodes present in that level. Example: Consider the image given below: Form the image above, Nodes at level 1 are : 16 Nodes at level 2 are : 10, 25 Nodes at level 3 are : 7, 15, 18, 30 Solution: This […]

Get Number of Nodes in a Binary Tree

Question: Given a binary tree, get the total number of nodes in it. Example: Consider the image given below: Form the image above, The total number of nodes are 7. Solution: This problem can be solved by recursive traversal. Assume if you have only one node, and no left and right node. Now, you will […]

Given a linked list, check if it is a palindrome or not. Solution in C++

Solution to this problem can be found in 3 steps: 1. First find the middle of the linked list 2. Reverse the linked list from the middle 3. Compare both of the linked lists. Solution in C++ #include<iostream> #include<vector> #include<string> using namespace std; struct Node { int data; Node *next; Node(int x) { data = […]

C++ program to implement queue using stacks.

So our queue will be having following below functions: my_push(n) This function should insert the element “n” at the end of the queue my_pop() This function should remove the element from the front of the queue my_peek() This function will return the front element isEmpty() This function will check if the queue is empty or […]