<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* createNode(vector<int>& ListNode_vec){

        TreeNode* new_node = NULL;
        if (ListNode_vec.size() == 0){
            return new_node;
        }
        
        int left_idx = 0;
        int right_idx = ListNode_vec.size() - 1;
        int mid_idx = left_idx + (right_idx - left_idx)/2;

        // root
        new_node = new TreeNode(ListNode_vec[mid_idx]);
        // printf("new_node->val:%d\n", new_node->val);

        if (left_idx == right_idx){
            return new_node;
        }
        
        
        // left-side, [vec[begin], vec[mid])
        if (left_idx < mid_idx)
        {
            vector<int> left_vec(ListNode_vec.begin(), ListNode_vec.begin() + mid_idx);
            auto left_node_ptr = createNode(left_vec);
            // if(left_node_ptr)
            //     printf("left_node_ptr->val:%d\n", left_node_ptr->val);
            new_node->left = left_node_ptr;
        }
        
        if (right_idx > mid_idx)
        {
            // right-side, (vec[mid], vec[end])
            vector<int> right_vec(ListNode_vec.begin() + mid_idx + 1, ListNode_vec.end());
            auto right_node_ptr = createNode(right_vec);
            // if(right_node_ptr)
            //     printf("right_node_ptr->val:%d\n", right_node_ptr->val);
            new_node->right = right_node_ptr;
        }

        return new_node;
        
    }

    TreeNode* sortedListToBST(ListNode* head) {
        // transfer node from LiskList to array
        ListNode *tmp_head = head;
        vector<int> ListNode_vec;
        while (tmp_head)
        {
            ListNode_vec.push_back(tmp_head->val);
            tmp_head = tmp_head->next;
        }
        // printf("ListNode_vec:\n");
        // for (size_t i = 0; i < ListNode_vec.size(); i++)
        // {
        //     printf("%d,", ListNode_vec[i]);
        // }
        // printf("\n");

        // 
        TreeNode *root = createNode(ListNode_vec);
        

        return root;
    }
};
37300