class Solution { ListNode lnode=null; public TreeNode sortedListToBST(ListNode head) { int size=0; ListNode p=head; while(p!=null) { p=p.next; size++; } lnode=head; return buildBST(size); } private TreeNode buildBST(int size){ if(size==0) return null; TreeNode tnode=new TreeNode(0); tnode.left=buildBST(size/2); tnode.val=lnode.val; lnode=lnode.next; tnode.right=buildBST(size-size/2-1); return tnode; }}