<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:
    TreeNode* sortedListToBST(ListNode* head) {
        if(head == nullptr) return nullptr;
        if(head->next == nullptr) return new TreeNode(head->val);
        
        vector<int> arr;
        ListNode* node = head;
        while(node != nullptr){
            arr.push_back(node->val);
            node = node->next;
        }
        int n = arr.size(); // n number of nodes
        
        int root_idx = (n - 1) / 2;
        TreeNode* root = new TreeNode(arr[root_idx]);
        vector<TreeNode*> parent;
        vector<char> child_type; // 'L' left 'R' right
        vector<vector<int>> ranges;
        if(root_idx > 0){
            parent.push_back(root);
            child_type.push_back('L');
            ranges.push_back({0, root_idx - 1});
        }if(root_idx != n - 1){
            parent.push_back(root);
            child_type.push_back('R');
            ranges.push_back({root_idx + 1, n - 1});
        }
        while(ranges.size() > 0){
            vector<vector<int>> next_ranges;
            vector<TreeNode*> next_parent;
            vector<char> next_child_type;
            for(int i = 0; i < ranges.size(); ++i){
                TreeNode* parent_node = parent[i];
                char c = child_type[i];
                int beg = ranges[i][0]; // [begin, end] interval
                int end = ranges[i][1];
                int node_idx = (beg + end) / 2;
                TreeNode* new_node = new TreeNode(arr[node_idx]);
                if(c == 'L') parent_node->left = new_node;
                else parent_node->right = new_node;
                if(node_idx > beg){
                    next_parent.push_back(new_node);
                    next_child_type.push_back('L');
                    next_ranges.push_back({beg, node_idx - 1});
                }
                if(node_idx != end){
                    next_parent.push_back(new_node);
                    next_child_type.push_back('R');
                    next_ranges.push_back({node_idx + 1, end});
                }
            }
            ranges = next_ranges;
            parent = next_parent;
            child_type = next_child_type;
        }
        return root;
    }
};
30700