struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode *sortedListToBST(ListNode* &ln, int len) {
if (len <= 0) return NULL;
TreeNode *lChild = sortedListToBST(ln, len/2);
TreeNode *tn = new TreeNode(ln->val);
tn->left = lChild;
ln = ln->next;
tn->right = sortedListToBST(ln, len-1-len/2);
return tn;
}
TreeNode *sortedListToBST(ListNode *head) {
ListNode *ln = head;
int len = 0;
while(ln != NULL) {
ln = ln->next;
++len;
}
ln = head;
sortedListToBST(ln, len);
}