Snippets Collections
class Solution{
    // Your are required to complete this function
    // function should return root of the modified BST
    void inorder(Node* root, vector<int>& bst){
        inorder(root->left, bst);
        inorder(root->right, bst);
    Node* build(int l , int r, vector<int>& bst){
        if(l > r)
            return nullptr;
        if(l == r)
            return new Node(bst[l]);
        int m = (l + r)/2;
        Node* root = new Node(bst[m]);
        root->left = build(l, m-1,bst);
        root->right = build(m+1,r,bst);
        return root;
    Node* buildBalancedTree(Node* root)
    	// Code here
    	vector<int> bst;
    	inorder(root, bst);
    	return build(0, bst.size() - 1, bst);

Sun Oct 15 2023 11:44:12 GMT+0000 (Coordinated Universal Time)

#bst #balancebst #easy

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension