forked from leetcoders/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvertSortedListtoBinarySearchTree.h
60 lines (56 loc) · 1.47 KB
/
ConvertSortedListtoBinarySearchTree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
Author: Annie Kim, [email protected]
Date: Apr 10, 2013
Update: Jul 29, 2013
Problem: Convert Sorted List to Binary Search Tree
Difficulty: Medium
Source: http://leetcode.com/onlinejudge#question_109
Notes:
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
Solution: Recursion. Pre-order. O(n)
*/
/**
* 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) {
return sortedListToBSTRe(head, getLength(head));
}
TreeNode *sortedListToBSTRe(ListNode *&head, int length)
{
if (length == 0) return NULL;
int mid = length / 2;
TreeNode *left = sortedListToBSTRe(head, mid);
TreeNode *root = new TreeNode(head->val);
TreeNode *right = sortedListToBSTRe(head->next, length - mid - 1);
root->left = left;
root->right = right;
head = head->next;
return root;
}
int getLength(ListNode *head)
{
int length = 0;
while (head)
{
length++;
head = head->next;
}
return length;
}
};