class Solution {
public TreeNode sortedListToBST(ListNode head) {
if(head==null)return null;
return create(head,null);
}
public TreeNode create(ListNode left,ListNode right){
if (left == right) {
return null;
}
ListNode fast=left;
ListNode slow=left;
while(fast.next!=right && fast.next.next!=right){
fast=fast.next.next;
slow=slow.next;
}
TreeNode mid=new TreeNode(slow.val);
mid.left=create(left,slow);
mid.right=create(slow.next,right);
return mid;
}
}
有序链表转换二叉搜索树109
最新推荐文章于 2024-07-24 17:27:32 发布