Table of contents
1.
Introduction
2.
Solution Approach
3.
C++ Code
4.
Complexity Analysis
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order

Author Ayush Prakash
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

 

Let us first understand the problem statement clearly. The problem is that we are given a binary search tree, and an integer ‘K’, ’K’ can be either 0 or 1. When:

K = 0, we must construct a right-skewed binary tree in increasing order.

K = 1, we must construct a right-skewed binary tree in decreasing order.

Let us understand the problem with the help of an example: 

 

Input: 

 

 

Output

case 1 : K = 0

 

 

case 2 : K = 1

 

Solution Approach

We do an inorder/reverse inorder traversal of the given binary search tree to obtain elements of the tree in increasing/decreasing order (based on the value of ‘K’).

In inorder traversal, we traverse the left subtree first, then the root and then finally the right subtree, whereas, in reverse inorder traversal, we do the reverse, that is, we traverse the right subtree first, then the root and then the left subtree. We do this to obtain the elements of the tree in sorted order. 

We traverse the given binary search tree and keep track of the previous node and adjust the left and the right pointers of the node accordingly to obtain the desired tree. 

C++ Code

 

#include <bits/stdc++.h>
using namespace std;

// Defining the structutre of TreeNode
struct TreeNode
{
   int data;
   TreeNode *left, *right;
   TreeNode(int x)
   {
       this->data = x;
       this->left = NULL;
       this->right = NULL;
   }
};

// Initializing global variables
TreeNode *headNode = NULL;
TreeNode *prevNode = NULL;

void BSTToSkewed(TreeNode *curr, int k)
{

   /*
   Base case, when the tree is empty
   we just return
   */
   if (curr == NULL)
   {
       return;
   }

   /*
   For k == 1, we need to construct
   the skewed tree in decresing order, so
   we traverse the right subtree first
   followed by the root and then the
   left subtree
   */
   if (k == 1)
   {
       BSTToSkewed(curr->right, k);
   }

   /*
   For k == 0, we need to construct
   the skewed tree in increasing order, so
   we traverse the left subtree first
   followed by the root and then the
   right subtree
   */
   else
   {
       BSTToSkewed(curr->left, k);
   }

   /*
   Storing the reference to
   the left and the right subtree
   so that we do not lose their
   refrence while adjusting the
   pointers
   */
   TreeNode *leftSubTree = curr->left;
   TreeNode *rightSubTree = curr->right;

   /*
   Adjusting the pointers
   to construct the desired tree (skewed)
   */
   if (headNode == NULL)
   {
       headNode = curr;
       prevNode = curr;
   }
   else
   {
       prevNode->right = curr;
       curr->left = NULL;
       prevNode = curr;
   }

   if (k == 1)
   {
       BSTToSkewed(leftSubTree, k);
   }
   else
   {
       BSTToSkewed(rightSubTree, k);
   }
}

// Entry point
int main()
{

   // Creating a binary search tree
   TreeNode *root = new TreeNode(10);
   root->left = new TreeNode(5);
   root->left->left = new TreeNode(1);
   root->left->right = new TreeNode(6);
   root->right = new TreeNode(12);
   root->right->left = new TreeNode(11);
   root->right->right = new TreeNode(15);

   /*
   Toggle between the lines 106 and 107
   by commenting/uncommenting to test both
   cases i.e. k = 0 or k = 1
   int k = 0;
   */
   int k = 1;

   /*
   Converting the binary search tree into a
   increasing / decreasing skewed tree
   */
   BSTToSkewed(root, k);

   // Traversing the skewed tree
   while (headNode != NULL)
   {
       cout << headNode->data << " ";
       headNode = headNode->right;
   }
   cout << endl;
}
You can also try this code with Online C++ Compiler
Run Code


Output

When K = 0, 

1 5 6 10 11 12 15

 

​​When K = 1, 

15 12 11 10 6 5 1

 

Complexity Analysis

Time complexity: Since we are doing an inorder traversal of the tree, the time complexity is O(N), where ‘N’ is the number of nodes in the tree. 

Space complexity: We are not using any extra space to solve the given problem; hence the space complexity is O(1). 

Note: We are not considering the recursion call stack in our space complexity analysis. 

FAQs

1.Can this problem be solved in less than O(N) time? 

  •  No! this is the most optimal solution to the problem. In order to solve this problem, we must visit every node in the tree; hence the time complexity is O(N) and can not be reduced any further. 

 

2.Which tree traversal technique is used for obtaining elements of a binary search tree in sorted order? 

  • Inorder traversal.  

 

Key Takeaways

In this blog, we solved a really interesting problem: “Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order”. We also discussed the time and space complexity of our solution. Remember, always try to find the time and space complexity of your solutions because you will be asked to find the complexities of your solutions in your interviews. If you love solving coding problems, please visit this incredible platform Coding Ninjas Studio.

Do check out our DSA course, to master data structures and algorithms and ace coding interviews.

Check out this problem - Diameter Of Binary Tree

If you feel that this blog has helped you in any way, then please do share it with your friends. 

Happy Coding!

Live masterclass