<p>Given the <code>head</code> of a singly linked list where elements are sorted in <strong>ascending order</strong>, convert <em>it to a </em><span data-keyword="height-balanced"><strong><em>height-balanced</em></strong></span> <em>binary search tree</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" />
<pre>
<strong>Input:</strong> head = [-10,-3,0,5,9]
<strong>Output:</strong> [0,-3,9,-10,null,5]
<strong>Explanation:</strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> head = []
<strong>Output:</strong> []
</pre>

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

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

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
/**
 * 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:
    int height(TreeNode *tree)
    {
        if (!tree)
            return -1;
        return max(height(tree->left), height(tree->right)) + 1;
    }
    TreeNode *rotateLeft(TreeNode *x)
    {
        TreeNode *y = x->right;
        TreeNode *t2 = y->left;
        x->right = t2;
        y->left = x;
        return y;
    }
    TreeNode *rotateRight(TreeNode *x)
    {
        TreeNode *y = x->left;
        TreeNode *t2 = y->right;
        x->left = t2;
        y->right = x;
        return y;
    }
    int getBalanceFactor(TreeNode *tree)
    {
        if (!tree)
            return 0;
        return height(tree->left) - height(tree->right);
    }
    void balance(TreeNode *&tree)
    {
        int balanceFactor = getBalanceFactor(tree);
        if (balanceFactor > 0)
        {
            if (getBalanceFactor(tree->left) >= 0)
            {
                tree = rotateRight(tree);
            }
            else
            {
                tree->left = rotateLeft(tree->left);
                tree = rotateRight(tree);
            }
        }
        if (balanceFactor < 0)
        {
            if (getBalanceFactor(tree->right) <= 0)
            {
                tree = rotateLeft(tree);
            }
            else
            {
                tree->right = rotateRight(tree->right);
                tree = rotateLeft(tree);
            }
        }
    }
    void insert(TreeNode *&head, int key)
    {
        if (!head)
        {
            head = new TreeNode{key, nullptr, nullptr};
            return;
        }
        if (head->val > key)
        {
            insert(head->left, key);
        }
        else if (head->val < key)
        {
            insert(head->right, key);
        }
        balance(head);
    }
    TreeNode *helper(vector<int> a)
    {
        if (a.size() == 0)
        {
            return nullptr;
        }
        int n = a.size();
        TreeNode *tree = new TreeNode(a[n / 2]);
        tree->left = helper(vector<int>(a.begin(), a.begin() + n / 2));
        tree->right = helper(vector<int>(a.begin() + n / 2 + 1, a.end()));
        return tree;
    }
    TreeNode* sortedListToBST(ListNode* head) {
        vector<int> arr;
        ListNode *temp = head;
        while (temp)
        {
            arr.push_back(temp->val);
            temp = temp->next;
        }
        TreeNode *tree = helper(arr);
        return tree;
    }
};
37000