Bottom-up 较难的题目
1. 和 google code jam 分数那题差不多, 需要用到 &
TreeNode* buildTree(ListNode* &head, int st, int ed) { if(st > ed) return NULL; int mid = (st+ed)>>1; TreeNode* lchild = buildTree(head, st, mid-1); TreeNode* parent = new TreeNode(head->val); head = head->next; TreeNode* rchild = buildTree(head, mid+1, ed); parent->left = lchild; parent->right = rchild; return parent; } class Solution { public: TreeNode *sortedListToBST(ListNode *head) { int len = 0; ListNode *cursor = head; while(cursor) { len ++; cursor = cursor->next; } if(len == 0) return NULL; cursor = head; return buildTree(cursor, 0, len-1); } };