Convert BST to Max Heap

Last Updated : 28 May, 2026

Given a Binary Search Tree which is also a Complete Binary Tree. The problem is to convert a given BST into a Special Max Heap with the condition that all the values in the left subtree of a node should be less than all the values in the right subtree of the node. This condition is applied to all the nodes in the so-converted Max Heap. 

Examples:  

Input:

frame_3348


Output:

frame_3349


Explanation: The given BST has been transformed into a Max Heap. All the nodes in the Max Heap satisfy the given condition, that is, values in the left subtree of a node should be less than the values in the right a subtree of the node.

Try It Yourself
redirect icon

Prerequisites: Binary Search Tree | Heaps 

Using Inorder + Postorder Traversal – O(n) Time and O(n) Space

The idea is to first store the BST elements in sorted order using inorder traversal. Since inorder traversal of a BST always produces sorted values, these values can later be reassigned to the tree in postorder fashion. Assigning values during postorder traversal ensures that every parent gets a larger value than its children, thereby converting the BST into a Max Heap while preserving the tree structure.

  • Perform inorder traversal of BST and store node values in a sorted array
  • Traverse the tree in postorder manner (left → right → root)
  • Replace node values sequentially using the sorted array
  • After reassignment, the tree satisfies Max Heap properties
C++
#include <bits/stdc++.h>
using namespace std;

class Node
{
public:
    int data;
    Node *left, *right;

    Node(int value)
    {
        data = value;
        left = nullptr;
        right = nullptr;
    }
};

// Function for the inorder traversal of the tree
// so as to store the node values in 'arr' in
// sorted order
void inorderTraversal(Node* root, vector<int>& arr)
{
    if (root == NULL)
        return;

    // first recur on left subtree
    inorderTraversal(root->left, arr);

    // then copy the data of the node
    arr.push_back(root->data);

    // now recur for right subtree
    inorderTraversal(root->right, arr);
}

void BSTToMaxHeap(Node* root, vector<int> &arr, int& i)
{
    if (root == NULL)
        return;

    // recur on left subtree
    BSTToMaxHeap(root->left, arr, i);

    // recur on right subtree
    BSTToMaxHeap(root->right, arr, i);

    // copy data at index 'i' of 'arr' to
    // the node
    root->data = arr[++i];
}

// Utility function to convert the given BST to
// MAX HEAP
void convertToMaxHeapUtil(Node* root)
{
    // vector to store the data of all the
    // nodes of the BST
    vector<int> arr;
    int i = -1;

    // inorder traversal to populate 'arr'
    inorderTraversal(root, arr);

    // BST to MAX HEAP conversion
    BSTToMaxHeap(root, arr, i);
}

// Function to Print Postorder Traversal of the tree
void postorderTraversal(Node* root)
{
    if (!root)
        return;

    // recur on left subtree
    postorderTraversal(root->left);

    // then recur on right subtree
    postorderTraversal(root->right);

    // print the root's data
    cout << root->data << " ";
}

// Driver Code
int main()
{
    // BST formation
    Node* root = new Node(4);

    root->left = new Node(2);
    root->right = new Node(6);

    root->left->left = new Node(1);
    root->left->right = new Node(3);

    root->right->left = new Node(5);
    root->right->right = new Node(7);

    convertToMaxHeapUtil(root);

    cout << "Postorder Traversal of Tree:" << endl;

    postorderTraversal(root);

    return 0;
}
Java
import java.util.*;

class Node
{
    int data;
    Node left, right;

    Node(int value)
    {
        data = value;
        left = null;
        right = null;
    }
}

class GFG {

    // Function for the inorder traversal of the tree
    // so as to store the node values in 'arr' in
    // sorted order
    static void inorderTraversal(Node root, ArrayList<Integer> arr)
    {
        if (root == null)
            return;

        // first recur on left subtree
        inorderTraversal(root.left, arr);

        // then copy the data of the node
        arr.add(root.data);

        // now recur for right subtree
        inorderTraversal(root.right, arr);
    }

    static void BSTToMaxHeap(Node root, ArrayList<Integer> arr, int[] i)
    {
        if (root == null)
            return;

        // recur on left subtree
        BSTToMaxHeap(root.left, arr, i);

        // recur on right subtree
        BSTToMaxHeap(root.right, arr, i);

        // copy data at index 'i' of 'arr' to
        // the node
        root.data = arr.get(++i[0]);
    }

    // Utility function to convert the given BST to
    // MAX HEAP
    static void convertToMaxHeapUtil(Node root)
    {
        // vector to store the data of all the
        // nodes of the BST
        ArrayList<Integer> arr = new ArrayList<>();
        int[] i = {-1};

        // inorder traversal to populate 'arr'
        inorderTraversal(root, arr);

        // BST to MAX HEAP conversion
        BSTToMaxHeap(root, arr, i);
    }

    // Function to Print Postorder Traversal of the tree
    static void postorderTraversal(Node root)
    {
        if (root == null)
            return;

        // recur on left subtree
        postorderTraversal(root.left);

        // then recur on right subtree
        postorderTraversal(root.right);

        // print the root's data
        System.out.print(root.data + " ");
    }

    // Driver Code
    public static void main(String[] args)
    {
        // BST formation
        Node root = new Node(4);

        root.left = new Node(2);
        root.right = new Node(6);

        root.left.left = new Node(1);
        root.left.right = new Node(3);

        root.right.left = new Node(5);
        root.right.right = new Node(7);

        convertToMaxHeapUtil(root);

        System.out.println("Postorder Traversal of Tree:");

        postorderTraversal(root);
    }
}
Python
class Node:

    def __init__(self, value):
        self.data = value
        self.left = None
        self.right = None


# Function for the inorder traversal of the tree
# so as to store the node values in 'arr' in
# sorted order
def inorderTraversal(root, arr):

    if root is None:
        return

    # first recur on left subtree
    inorderTraversal(root.left, arr)

    # then copy the data of the node
    arr.append(root.data)

    # now recur for right subtree
    inorderTraversal(root.right, arr)


def BSTToMaxHeap(root, arr, i):

    if root is None:
        return

    # recur on left subtree
    BSTToMaxHeap(root.left, arr, i)

    # recur on right subtree
    BSTToMaxHeap(root.right, arr, i)

    # copy data at index 'i' of 'arr' to
    # the node
    i[0] += 1
    root.data = arr[i[0]]


# Utility function to convert the given BST to
# MAX HEAP
def convertToMaxHeapUtil(root):

    # vector to store the data of all the
    # nodes of the BST
    arr = []
    i = [-1]

    # inorder traversal to populate 'arr'
    inorderTraversal(root, arr)

    # BST to MAX HEAP conversion
    BSTToMaxHeap(root, arr, i)


# Function to Print Postorder Traversal of the tree
def postorderTraversal(root):

    if not root:
        return

    # recur on left subtree
    postorderTraversal(root.left)

    # then recur on right subtree
    postorderTraversal(root.right)

    # print the root's data
    print(root.data, end=" ")


# Driver Code

# BST formation
root = Node(4)

root.left = Node(2)
root.right = Node(6)

root.left.left = Node(1)
root.left.right = Node(3)

root.right.left = Node(5)
root.right.right = Node(7)

convertToMaxHeapUtil(root)

print("Postorder Traversal of Tree:")

postorderTraversal(root)
C#
using System;
using System.Collections.Generic;

class Node
{
    public int data;
    public Node left, right;

    public Node(int value)
    {
        data = value;
        left = null;
        right = null;
    }
}

class GFG
{
    // Function for the inorder traversal of the tree
    // so as to store the node values in 'arr' in
    // sorted order
    static void inorderTraversal(Node root, List<int> arr)
    {
        if (root == null)
            return;

        // first recur on left subtree
        inorderTraversal(root.left, arr);

        // then copy the data of the node
        arr.Add(root.data);

        // now recur for right subtree
        inorderTraversal(root.right, arr);
    }

    static void BSTToMaxHeap(Node root, List<int> arr, ref int i)
    {
        if (root == null)
            return;

        // recur on left subtree
        BSTToMaxHeap(root.left, arr, ref i);

        // recur on right subtree
        BSTToMaxHeap(root.right, arr, ref i);

        // copy data at index 'i' of 'arr' to
        // the node
        root.data = arr[++i];
    }

    // Utility function to convert the given BST to
    // MAX HEAP
    static void convertToMaxHeapUtil(Node root)
    {
        // vector to store the data of all the
        // nodes of the BST
        List<int> arr = new List<int>();
        int i = -1;

        // inorder traversal to populate 'arr'
        inorderTraversal(root, arr);

        // BST to MAX HEAP conversion
        BSTToMaxHeap(root, arr, ref i);
    }

    // Function to Print Postorder Traversal of the tree
    static void postorderTraversal(Node root)
    {
        if (root == null)
            return;

        // recur on left subtree
        postorderTraversal(root.left);

        // then recur on right subtree
        postorderTraversal(root.right);

        // print the root's data
        Console.Write(root.data + " ");
    }

    // Driver Code
    static void Main()
    {
        // BST formation
        Node root = new Node(4);

        root.left = new Node(2);
        root.right = new Node(6);

        root.left.left = new Node(1);
        root.left.right = new Node(3);

        root.right.left = new Node(5);
        root.right.right = new Node(7);

        convertToMaxHeapUtil(root);

        Console.WriteLine("Postorder Traversal of Tree:");

        postorderTraversal(root);
    }
}
JavaScript
class Node
{
    constructor(value)
    {
        this.data = value;
        this.left = null;
        this.right = null;
    }
}

// Function for the inorder traversal of the tree
// so as to store the node values in 'arr' in
// sorted order
function inorderTraversal(root, arr)
{
    if (root == null)
        return;

    // first recur on left subtree
    inorderTraversal(root.left, arr);

    // then copy the data of the node
    arr.push(root.data);

    // now recur for right subtree
    inorderTraversal(root.right, arr);
}

function BSTToMaxHeap(root, arr, i)
{
    if (root == null)
        return;

    // recur on left subtree
    BSTToMaxHeap(root.left, arr, i);

    // recur on right subtree
    BSTToMaxHeap(root.right, arr, i);

    // copy data at index 'i' of 'arr' to
    // the node
    root.data = arr[++i.value];
}

// Utility function to convert the given BST to
// MAX HEAP
function convertToMaxHeapUtil(root)
{
    // vector to store the data of all the
    // nodes of the BST
    let arr = [];
    let i = { value: -1 };

    // inorder traversal to populate 'arr'
    inorderTraversal(root, arr);

    // BST to MAX HEAP conversion
    BSTToMaxHeap(root, arr, i);
}

// Function to Print Postorder Traversal of the tree
function postorderTraversal(root)
{
    if (!root)
        return;

    // recur on left subtree
    postorderTraversal(root.left);

    // then recur on right subtree
    postorderTraversal(root.right);

    // print the root's data
    process.stdout.write(root.data + " ");
}

// Driver Code

// BST formation
let root = new Node(4);

root.left = new Node(2);
root.right = new Node(6);

root.left.left = new Node(1);
root.left.right = new Node(3);

root.right.left = new Node(5);
root.right.right = new Node(7);

convertToMaxHeapUtil(root);

console.log("Postorder Traversal of Tree:");

postorderTraversal(root);

Output
Postorder Traversal of Tree:
1 2 3 4 5 6 7 
Comment