-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path109-Convert-Sorted-List-to-Binary-Search-Tree.cpp
More file actions
41 lines (34 loc) · 1.38 KB
/
Copy path109-Convert-Sorted-List-to-Binary-Search-Tree.cpp
File metadata and controls
41 lines (34 loc) · 1.38 KB
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
class Solution {
public:
TreeNode* sortedListToBST(ListNode* head) {
if(head) return dfs(head, nullptr);
return nullptr;
}
TreeNode* dfs(ListNode* head, ListNode* tail){
if(head == tail) return nullptr;
if(head->next == tail){
TreeNode* node = new TreeNode(head->val);
return node;
}
ListNode* fast = head;
ListNode* slow = head;
while(fast != tail && fast->next != tail){
slow = slow->next;
fast = fast->next->next;
}
TreeNode* node = new TreeNode(slow->val);
node->left = dfs(head, slow);
node->right = dfs(slow->next, tail);
return node;
}
};
/* 109. Convert-Sorted-List-to-Binary-Search-Tree.cpp
//////////////////////////////////////////////////
Given the head of a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
Aa height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
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.
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/
//////////////////////////////////////////////////
*/