int length(ListNode node){
int count = 0;
if(node == null){
return count;
}else{
count++;
}
while(node.getNext() != null){
count++;
node = node.getNext();
}
return count;
}
BinaryTreeNode sortedListToBST(listNode node){
return sortedListToBST(node,0,length(node)-1);
}
BinaryTreeNode sortedListToBST(ListNode node,int start,int end){
if(node == null || start > end){
return null;
}
int mid = start+(end-start)/2;
BinaryTreeNode left = sortedListToBST(node,start,mid-1);
BinaryTreeNode parent = new BinaryTreeNode();
if(parent == null){
System.out.println("Memory error!");
}
parent.setData(node.getData());
parent.setLeft(left);
parent.setRight(node.getNext(),mid+1,end);
return parent;
}
10-11
832