题目:
题解:
class Solution {
public:int getLength(ListNode* head) {int ret = 0;for (; head != nullptr; ++ret, head = head->next);return ret;}TreeNode* buildTree(ListNode*& head, int left, int right) {if (left > right) {return nullptr;}int mid = (left + right + 1) / 2;TreeNode* root = new TreeNode();root->left = buildTree(head, left, mid - 1);root->val = head->val;head = head->next;root->right = buildTree(head, mid + 1, right);return root;}TreeNode* sortedListToBST(ListNode* head) {int length = getLength(head);return buildTree(head, 0, length - 1);}
};