1. 链表-->BST (借鉴smile的)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *sortedListToBST(ListNode *head) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int cnt = 0;
ListNode* cur = head;
while (cur)
{
cur = cur->next;
cnt++;
}
return build(head, 0, cnt - 1);
}
TreeNode* build(ListNode* &head, int left, int right)
{
if (left > right)
return NULL;