leetcode 109. Convert Sorted List to Binary Search Tree

来源:互联网 发布:vscode提交到github 编辑:程序博客网 时间:2024/05/23 19:09
class Solution {public:TreeNode* sortedListToBST(ListNode* head){return process(head, nullptr);}private:TreeNode* process(ListNode* start, ListNode* end){if (start == end){return nullptr;}auto *fast = start, *slow = start;while (fast != end && fast->next != end){slow = slow->next;fast = fast->next->next;}auto root = new TreeNode(slow->val);root->left = process(start, slow);root->right = process(slow->next, end);return root;}};

0 0
原创粉丝点击