forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertSortedListToBinarySearchTree.cpp
122 lines (99 loc) · 2.5 KB
/
convertSortedListToBinarySearchTree.cpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Source : https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/
// Author : Hao Chen
// Date : 2014-07-03
/**********************************************************************************
*
* Given a singly linked list where elements are sorted in ascending order,
* convert it to a height balanced BST.
*
**********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <queue>
using namespace std;
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(int low, int high, ListNode*& head);
TreeNode *sortedListToBST(ListNode *head) {
int len = 0;
for(ListNode* p=head; p!=NULL; p=p->next){
len++;
}
return sortedListToBST(0, len-1, head);
}
TreeNode* sortedListToBST(int low, int high, ListNode*& head) {
if (low>high || head==NULL) return NULL;
int mid = low + (high - low)/2;
TreeNode* leftNode = sortedListToBST(low, mid-1, head);
TreeNode* node = new TreeNode(head->val);
node->left = leftNode;
head = head->next;
TreeNode* rightNode = sortedListToBST(mid+1, high, head);
node->right = rightNode;
return node;
}
void printTree_level_order(TreeNode *root)
{
queue<TreeNode*> q;
q.push(root);
while (q.size()>0){
TreeNode* n = q.front();
q.pop();
if (n==NULL){
cout << "# ";
continue;
}
cout << n->val << " ";
q.push(n->left);
q.push(n->right);
}
cout << endl;
}
ListNode* createList(int a[], int n)
{
ListNode *head=NULL, *p=NULL;
for(int i=0; i<n; i++){
if (head == NULL){
head = p = new ListNode(a[i]);
}else{
p->next = new ListNode(a[i]);
p = p->next;
}
}
return head;
}
void printList(ListNode* h)
{
while(h!=NULL){
printf("%d -> ", h->val);
h = h->next;
}
printf("NULL\n");
}
int main(int argc, char** argv)
{
int n = 8;
if (argc>1){
n = atoi(argv[1]);
}
int *a = new int[n];
for(int i=0; i<n; i++){
a[i]=i+1;
}
ListNode* head = createList(a, n);
printList(head);
TreeNode* root = sortedListToBST(head);
printTree_level_order(root);
delete[] a;
return 0;
}