BST to max heap | Practice | GeeksforGeeks

Given a Binary Search Tree. 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 on all the nodes in the so converted Max Heap.
Example 1:
Input :
                 4
               /   \
              2     6
            /  \   /  \
           1   3  5    7  

Output : 1 2 3 4 5 6 7 
Exaplanation :
               7
             /   \
            3     6
          /   \  /   \
         1    2 4     5
The given BST has been transformed into a
Max Heap and it's postorder traversal is
1 2 3 4 5 6 7.
0
patildhiren441 month ago
static ArrayList<Integer> al ;
    
    static void inorder(Node root){
        if(root==null){
            return;
        }
        
        inorder(root.left);
        al.add(root.data);
        inorder(root.right);
    }
    
    static void postorder(Node root){
        if(root==null){
            return;
        }
        
        postorder(root.left);
        postorder(root.right);
        
        root.data = al.get(0);
        al.remove(0);
    }
    
    public static void convertToMaxHeapUtil(Node root)
    {
        //code here
        al = new ArrayList<>();
        
        inorder(root);
        postorder(root);
        
    }
0
aliaksandrhn1 month ago
Can anybody explain to me the reason GeeksForGeeks uses static methods in many of its problems? Static methods often require the use of global static variables which causes test cases to fail (this, by the way, does not happen on other sites like LeetCode where the use of static variables works just fine). When you get rid of all static methods on GeeksForGeeks all tests pass. What's the point of using static methods?
0
himanshukug19cs
This comment was deleted.
+1
aaishuagarwal3 months ago
simply use priority queue
public: priority_queue<int,vector<int>>pq; void p(Node * root) {     if(root==NULL)     return ;     pq.push(root->data);     p(root->left);     p(root->right); } void in(Node * root) {     if(root==NULL||pq.empty()==true)     return ;    auto x=pq.top();           pq.pop();           root->data=x;   in(root->right);   in(root->left); }   void convertToMaxHeapUtil(Node* root)   {       // Your code goes here       p(root);        in(root);   }    
+1
himanshukug19cs4 months ago
java code
static void inorder(Node root,ArrayList<Integer> al){       if(root==null)          return;       inorder(root.left,al);       al.add(root.data);       inorder(root.right,al);       return;   }   static int i=0;   static void sol(Node root,ArrayList<Integer> al){  if(root==null)      return ;sol(root.left,al);sol(root.right,al); root.data=al.get(0); al.remove(al.get(0));return;}   public static void convertToMaxHeapUtil(Node root)   {       //code here       if(root==null)          return ;                    ArrayList<Integer> al = new ArrayList<>();          inorder(root,al);          sol(root,al);          return;   }
-1
punitkumarojha5 months ago
Using a simple Queue to solve the problem
class Solution{
    queue<int> q;
public:
    void dfs(Node* root){
        if(root==NULL)  
            return;
        dfs(root->right);
        q.push(root->data); // storing in descending order
        dfs(root->left);
    }
    
    void solver(Node* root){
        if(root==NULL)
            return;
        int val=q.front();
        q.pop();
        root->data=val; // putting the values according to defintion of the problem
        solver(root->right);
        solver(root->left);
    }
    
    void convertToMaxHeapUtil(Node* root){
        dfs(root);
        solver(root);
    }    
};
+2
anutiger6 months ago
    vector<int> ret;
    int ind = 0;
    void helper(Node *root){
        if(!root)
            return;
        helper(root->left);
        ret.push_back(root->data);
        helper(root->right);
    }
    void help(Node *root){
        if(!root) return ;
        root->data = ret[ind--];
        help(root->right);
        help(root->left);
    }
    void convertToMaxHeapUtil(Node* root)
    {
        ret.clear();
        helper(root);
        ind = ret.size() - 1;
        help(root);
    } 
0
Prashant Giri9 months ago
Prashant Giri
class Solution{//postorder traversal    vector<int>v;void update(Node* root , int &n){    if(!root) return;     update(root -> left , n);    update(root -> right , n);    root -> data = v[n++];}void inorder(Node *root){    if(!root){ //return NULL vector    return;    }    inorder(root -> left );    v.push_back(root -> data);    inorder(root -> right );    return;}public:void convertToMaxHeapUtil(Node* root){    //take the inorder traversal of the tree    //store it in a vector    //Now update the tree in the postorder manner    Node *temp = root;    inorder(temp );    int n = 0;    update(root,n);    }};
0
Shubham Kumar10 months ago
Shubham Kumar
0
Debojyoti Sinha1 year ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:0.42
class Solution{    void inOrder(Node *root, vector<int> &inOrderVal)    {        if(root)        {            inOrder(root->left, inOrderVal);                        inOrderVal.push_back(root->data);                        inOrder(root->right, inOrderVal);        }    }        void BSTToMaxHeap(Node *root, vector<int> &inOrderVal, int &index)    {        if(root)        {            BSTToMaxHeap(root->left,  inOrderVal, index);                        BSTToMaxHeap(root->right, inOrderVal, index);                        root->data = inOrderVal[index++];        }    }        public:    void convertToMaxHeapUtil(Node *root)    {        vector<int> inOrderVal;                inOrder(root, inOrderVal);                int index = 0;                BSTToMaxHeap(root, inOrderVal, index);    }    };
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.