Given a Binary Search Tree(BST), convert it to a Binary Tree such that every key of the original BST is changed to key plus sum of all smaller keys in BST.
Given a BST with N Nodes we have to convert it into Binary Tree
Given above BST with N=5 Nodes. The values at Node being 9, 6, 15, 3, 21
Binary Tree after conversion
Binary Tree after conversion, the values at Node being 18, 9, 33, 3, 54
Solution: We will perform a regular Inorder traversal in which we keep track of sum of Nodes visited. Let this sum be sum. The Node which is being visited, add that key of Node to sum i.e. sum = sum + Node->key. Change the key of the current Node to sum i.e. Node->key = sum.
When a BST is being traversed in inorder, for every key currently being visited, all keys that are already visited are all smaller keys.
Implementation:
C++
#include <bits/stdc++.h>
struct Node {
int key;
struct Node* left;
struct Node* right;
};
struct Node* newNode( int key)
{
struct Node* node = new Node;
node->key = key;
node->left = NULL;
node->right = NULL;
return (node);
}
void addSmallerUtil( struct Node* root, int * sum)
{
if (root == NULL)
return ;
addSmallerUtil(root->left, sum);
*sum = *sum + root->key;
root->key = *sum;
addSmallerUtil(root->right, sum);
}
void addSmaller( struct Node* root)
{
int sum = 0;
addSmallerUtil(root, &sum);
}
void printInorder( struct Node* node)
{
if (node == NULL)
return ;
printInorder(node->left);
printf ( "%d " , node->key);
printInorder(node->right);
}
int main()
{
Node* root = newNode(9);
root->left = newNode(6);
root->right = newNode(15);
printf ( " Original BST\n" );
printInorder(root);
addSmaller(root);
printf ( "\n BST To Binary Tree\n" );
printInorder(root);
return 0;
}
|
Java
class Node {
int data;
Node left, right;
Node( int d)
{
data = d;
left = right = null ;
}
}
class Sum {
int addvalue = 0 ;
}
class BSTtoBinaryTree {
static Node root;
Sum add = new Sum();
void addSmallerUtil(Node node, Sum sum)
{
if (node == null ) {
return ;
}
addSmallerUtil(node.left, sum);
sum.addvalue = sum.addvalue + node.data;
node.data = sum.addvalue;
addSmallerUtil(node.right, sum);
}
Node addSmaller(Node node)
{
addSmallerUtil(node, add);
return node;
}
void printInorder(Node node)
{
if (node == null ) {
return ;
}
printInorder(node.left);
System.out.print(node.data + " " );
printInorder(node.right);
}
public static void main(String[] args)
{
BSTtoBinaryTree tree = new BSTtoBinaryTree();
tree.root = new Node( 9 );
tree.root.left = new Node( 6 );
tree.root.right = new Node( 15 );
System.out.println( "Original BST" );
tree.printInorder(root);
Node Node = tree.addSmaller(root);
System.out.println( "" );
System.out.println( "BST To Binary Tree" );
tree.printInorder(Node);
}
}
|
Python3
class Node:
def __init__( self , data):
self .key = data
self .left = None
self .right = None
def addSmallerUtil(root, Sum ):
if root = = None :
return
addSmallerUtil(root.left, Sum )
Sum [ 0 ] = Sum [ 0 ] + root.key
root.key = Sum [ 0 ]
addSmallerUtil(root.right, Sum )
def addSmaller(root):
Sum = [ 0 ]
addSmallerUtil(root, Sum )
def printInorder(node):
if node = = None :
return
printInorder(node.left)
print (node.key, end = " " )
printInorder(node.right)
if __name__ = = '__main__' :
root = Node( 9 )
root.left = Node( 6 )
root.right = Node( 15 )
print ( "Original BST" )
printInorder(root)
print ()
addSmaller(root)
print ( "BST To Binary Tree" )
printInorder(root)
|
C#
using System;
public class Node
{
public int data;
public Node left, right;
public Node( int d)
{
data = d;
left = right = null ;
}
}
public class Sum
{
public int addvalue = 0;
}
public class BSTtoBinaryTree
{
public static Node root;
public Sum add = new Sum();
public virtual void addSmallerUtil(Node node, Sum sum)
{
if (node == null )
{
return ;
}
addSmallerUtil(node.left, sum);
sum.addvalue = sum.addvalue + node.data;
node.data = sum.addvalue;
addSmallerUtil(node.right, sum);
}
public virtual Node addSmaller(Node node)
{
addSmallerUtil(node, add);
return node;
}
public virtual void printInorder(Node node)
{
if (node == null )
{
return ;
}
printInorder(node.left);
Console.Write(node.data + " " );
printInorder(node.right);
}
public static void Main( string [] args)
{
BSTtoBinaryTree tree = new BSTtoBinaryTree();
BSTtoBinaryTree.root = new Node(9);
BSTtoBinaryTree.root.left = new Node(6);
BSTtoBinaryTree.root.right = new Node(15);
Console.WriteLine( "Original BST" );
tree.printInorder(root);
Node Node = tree.addSmaller(root);
Console.WriteLine( "" );
Console.WriteLine( "BST To Binary Tree" );
tree.printInorder(Node);
}
}
|
Javascript
<script>
class Node
{
constructor(d)
{
this .data = d;
this .left = null ;
this .right = null ;
}
}
class Sum
{
constructor()
{
this .addvalue = 0;
}
}
var root = null ;
var add = new Sum();
function addSmallerUtil(node, sum)
{
if (node == null )
{
return ;
}
addSmallerUtil(node.left, sum);
sum.addvalue = sum.addvalue + node.data;
node.data = sum.addvalue;
addSmallerUtil(node.right, sum);
}
function addSmaller(node)
{
addSmallerUtil(node, add);
return node;
}
function printInorder(node)
{
if (node == null )
{
return ;
}
printInorder(node.left);
document.write(node.data + " " );
printInorder(node.right);
}
root = new Node(9);
root.left = new Node(6);
root.right = new Node(15);
document.write( "Original BST<br>" );
printInorder(root);
var node = addSmaller(root);
document.write( "<br>" );
document.write( "BST To Binary Tree<br>" );
printInorder(node);
</script>
|
Output
Original BST
6 9 15
BST To Binary Tree
6 15 30
Time Complexity: O(n), the time complexity of this algorithm is O(n) because we traverse through the entire tree in order to add the sum of all smaller keys.
Auxiliary Space: O(H), Where H is the height of tree
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
09 Apr, 2023
Like Article
Save Article