109. Convert Sorted List to Binary Search Tree

109. Convert Sorted List to Binary Search Tree

Description

Given the head of a singly linked list where elements are sorted in ascending order , convert it to a height-balanced binary search tree.

Example 1:

1
2
3
Input: head = [-10,-3,0,5,9]
Output: [0,-3,9,-10,null,5]
Explanation: One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST.

Example 2:

1
2
Input: head = []
Output: []

Constraints:

  • The number of nodes in head is in the range [0, 2 * 10^4].
  • -10^5 <= Node.val <= 10^5

Hints/Notes

  • 2024/07/07
  • linked list and binary search tree
  • No solution from 0x3F

Solution

Language: C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* 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:
ListNode* cur;

TreeNode* sortedListToBST(ListNode* head) {
int len = 0;
for (ListNode* p = head; p; p = p->next) {
len++;
}
cur = head;
return traverse(0, len - 1);
}

TreeNode* traverse(int start, int end) {
if (start > end) {
return nullptr;
}
int mid = (end - start) / 2 + start;
TreeNode* l = traverse(start, mid - 1);
TreeNode* root = new TreeNode(cur->val);
cur = cur->next;
TreeNode* r = traverse(mid + 1, end);
root->left = l;
root->right = r;
return root;
}
};