-
Notifications
You must be signed in to change notification settings - Fork 213
/
ConvertSortedListtoBinarySearchTree.h
82 lines (77 loc) · 2.18 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
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: 1. Recursion. Pre-order. A very good Idea. O(n)
2. Recursion, Binary Search.
*/
/**
* 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_1(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;
}
TreeNode *sortedListToBST_2(ListNode *head) {
if(head==nullptr) return nullptr;
if(head->next==nullptr) return new TreeNode(head->val);
ListNode * slow = head;
ListNode * fast = head;
ListNode * pre = nullptr;
while(fast->next&&fast->next->next){
pre = slow;
slow = slow->next;
fast = fast->next->next;
}
fast = slow->next;
TreeNode * node = new TreeNode(slow->val);
if(pre){
pre->next = nullptr;
node->left = sortedListToBST(head);
}
node->right = sortedListToBST(fast);
return node;
}
};