<p>Given the <code>root</code> of a binary search tree, return <em>a <strong>balanced</strong> binary search tree with the same node values</em>. If there is more than one answer, return <strong>any of them</strong>.</p>

<p>A binary search tree is <strong>balanced</strong> if the depth of the two subtrees of every node never differs by more than <code>1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/10/balance1-tree.jpg" style="width: 500px; height: 319px;" />
<pre>
<strong>Input:</strong> root = [1,null,2,null,3,null,4,null,null]
<strong>Output:</strong> [2,1,3,null,null,null,4]
<b>Explanation:</b> This is not the only correct answer, [3,1,4,null,2] is also correct.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/10/balanced2-tree.jpg" style="width: 224px; height: 145px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> [2,1,3]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
	<li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
</ul>

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    // vector<TreeNode> nodes;
    TreeNode* balanceBST(TreeNode* root) {
        // vector<TreeNode> inOrder;
        vector<int> inOrder;
        inOrder = inOrderTraversal(root);
        vector<TreeNode*> nodes;
        for (auto &iter : inOrder) {
            nodes.push_back(new TreeNode(iter));
        }
        for (auto n : inOrder) {
            cout << n << " ";
        }
        cout << endl;
        int start = 0, end = inOrder.size() - 1;
        return createInOrder(nodes, start, end);
        // return root;
    }
private:
     vector<int> inOrderTraversal(TreeNode* root) {
        vector<int> traversed;
        if (root == nullptr) {
            return traversed;
        }
        vector<int> leftTraversed = inOrderTraversal(root->left);
        traversed.insert(traversed.end(), leftTraversed.begin(), leftTraversed.end());

        traversed.push_back(root->val);

        vector<int> rightTraversed = inOrderTraversal(root->right);
        traversed.insert(traversed.end(), rightTraversed.begin(), rightTraversed.end());

        return traversed;
     }
     TreeNode* createInOrder(vector<TreeNode*> &nodes, int startIdx, int endIdx) {
        // f(x) = f(root->left) + f(root) + f(root->right)
        TreeNode *root = nullptr;
        // exception
        // only one node
        // if (startIdx > endIdx) {
        //     return nullptr;
        // }
        if (startIdx == endIdx) {
            // return &(nodes[startIdx]);
            return (nodes[startIdx]);
        }

        // only two nodes
        if (startIdx + 1 == endIdx) {
            // nodes[endIdx].left = &(nodes[startIdx]);
            nodes[endIdx]->left = (nodes[startIdx]);
            // return &(nodes[endIdx]);
            return (nodes[endIdx]);
        }

        // general case
        int rootIdx = startIdx + (endIdx - startIdx + 1) / 2;
        // int rootIdx = startIdx + (endIdx - startIdx) / 2;
        // root = &(nodes[rootIdx]);
        root = (nodes[rootIdx]);

        root->left = createInOrder(nodes, startIdx, rootIdx - 1);
        root->right = createInOrder(nodes, rootIdx + 1, endIdx);

        return root;
     }
};
107268