public TreeNode sortedListToBST(ListNode head) {
if(head==null) return new TreeNode(0);
ArrayList<TreeNode> arr=new ArrayList<TreeNode>();
while(head!=null)
{
arr.add(new TreeNode(head.val));
head=head.next;
} return BST(arr,0,arr.size()-1);
}
TreeNode BST(ArrayList<TreeNode> list,int start,int end)
{
if(start>=end)
{
return list.get(start);
}
else
{
int mid=start+(end-start)/2;
TreeNode root=list.get(mid);
root.left=BST(list,start,mid-1);
root.right=BST(list,mid+1,end); return root;
}
}
相关文章
- 01-06Convert Sorted Array to Binary Search Tree
- 01-06【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
- 01-06LeetCode不定时刷题——Convert Sorted Array to Binary Search Tree
- 01-06[LeetCode] 426. Convert Binary Search Tree to Sorted Doubly Linked List
- 01-06leetcode -day19 Convert Sorted List to Binary Search Tree
- 01-06【leetcode】Convert Sorted List to Binary Search Tree
- 01-06【题解】【BST】【Leetcode】Convert Sorted Array to Binary Search Tree
- 01-06【LeetCode】108. Convert Sorted Array to Binary Search Tree
- 01-06108. Convert Sorted Array to Binary Search Tree*
- 01-06Convert Sorted Array to Binary Search Tree