
class Solution {
public:
TreeNode* build(vector<int>&val,int start,int end){
int mid=start+end>>1;
if(start>end)return nullptr;
TreeNode *head=new TreeNode(val[mid]);
head->left=build(val,start,mid-1);
head->right=build(val,mid+1,end);
return head;
}
TreeNode* sortedListToBST(ListNode* head) {
vector<int>val;
while(head){
val.push_back(head->val);
head=head->next;
}
return build(val,0,val.size()-1);
}
};
原创
leetcode109. 有序链表转换二叉搜索树
本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。