Convert Sorted List to Binary Search Tree java

 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;
}
}
上一篇:C# 序列化xml


下一篇:[asp.net mvc 奇淫巧技] 05 - 扩展ScriptBundle,支持混淆加密javascript