109. Convert Sorted List to Binary Search Tree 發表於 2023-02-13 | 分類於 leetcode problemsolution123456789101112131415161718class Solution {public: TreeNode* sortedListToBST(ListNode* head) { if(!head) return nullptr; if(!head->next) return new TreeNode(head->val); ListNode *slow = head, *fast = head, *pre = head; while(fast && fast->next){ pre = slow; fast =fast->next->next; slow = slow->next; } pre->next = nullptr; TreeNode *root = new TreeNode(slow->val); root->left = sortedListToBST(head); if(slow->next) root->right = sortedListToBST(slow->next); return root; }}; analysis time complexity O(n) space complexity O(n)