Skip to main content

Posts

Showing posts with the label recursion

Right view of a Binary Tree

In this post, we will discuss a method to print the Right view of a binary tree . This is one the most common easy level interview questions. Also, you might get to see interview questions which can be solved with slight variations to this code. The approach we take here is almost very similar to printing the Left view of the Binary Tree The right view contains all nodes that are last nodes in their levels. Right View of a tree is defined as the nodes that would be visible to us from the right side. To do this we have to simply print the first node encountered while traversing a level from the right. If you have seen my post for the left view of the binary tree then the only change that we make here is to traverse the right child of the current node first and then traverse the left node . Have a look at the below image to get some idea. Right View of a Binary Tree There are 2 solutions to this problem, first is to do a recursive preorder traversal in which we mainta...

Left view of a Binary Tree

In this post, we will discuss a method to print the left view of a binary tree. This is one the most common easy level interview questions. Also, you might get to see interview questions which can be solved with slight variations to this code. First, let's see what do we mean by the left view of a binary tree. The left view contains all nodes that are first nodes in their levels. Left View of a tree is defined as the nodes that would be visible to us from the left side. To do this we have to simply print the first node encountered while traversing a level from the left. Have a look at the below image to get some idea. Left view of a binary tree There are 2 solutions to this problem, first is to do a recursive preorder traversal in which we maintain max level we have traversed and then print the node if the max level is less than the max_level variable and update the max_level variable, another simple solution is to do level order traversal and print the first node in ev...

Quick Sort Algorithm using Recursion

Quick sort is one of the most important and widely used sorting algorithm. Quick sort is a very efficient algorithm which if implemented well can run two to three times faster than its competitors merge sort and and heap sort. The algorithm is a divide and conquer algorithm. The algorithm works by selecting a pivot element, this pivot element is usually the first element and choice of the pivot element affects the running time of the algorithm. The algorithm is basically to select a pivot element and then moving all the element less than the pivot element to its left and all the elements greater than pivot element to its right. Then we divide the array into 2 parts, one on the left of the pivot and other on the right of the pivot and then pass these 2 arrays to the algorithm for further sorting. Algorithm : Choose a Pivot element. Take 2 variables left_marker and right_marker excluding the pivot element. Set left_marker as the first element of the array Set right_marker as the...