-
Notifications
You must be signed in to change notification settings - Fork 0
/
1008 Construct Binary Search Tree from Preorder Traversal.c
134 lines (112 loc) · 3.56 KB
/
1008 Construct Binary Search Tree from Preorder Traversal.c
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
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/*
* Algorithm : Maintain a stack
* 1. If the preorder[i] value is less than node, insert it to left
* 2. If the preorder[i] value is more than node, check the value with the previous node from stack. If the value lies in between, then insert it to the right of current node
* Else pop out the previous node and compare the value from the previous nodes in stack, if it lies in between, then insert to the right of current node.
* 3. If stack is empty, insert it to the right of the current node.
*/
#define MAX_NODES 100
typedef struct
{
struct TreeNode * arr[MAX_NODES];
int top;
}Stack;
int isStackEmpty(Stack * tmp_stack)
{
if(tmp_stack->top == -1)
return 1;
else
return 0;
}
int isStackFull(Stack * tmp_stack)
{
if(tmp_stack->top == MAX_NODES-1)
return 1;
else
return 0;
}
void push(Stack * tmp_stack, struct TreeNode * element)
{
if(isStackFull(tmp_stack))
return;
tmp_stack->arr[++(tmp_stack->top)] = element;
}
struct TreeNode * pop(Stack * tmp_stack)
{
if(isStackEmpty(tmp_stack))
return NULL;
return tmp_stack->arr[(tmp_stack->top)--];
}
struct TreeNode * peek(Stack * tmp_stack, int index)
{
return tmp_stack->arr[index];
}
struct TreeNode* bstFromPreorder(int* preorder, int preorderSize)
{
Stack tmp_stack;
tmp_stack.top = -1;
struct TreeNode * root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
root->val = preorder[0];
root->left = NULL;
root->right = NULL;
struct TreeNode * trav_ptr = root;
int i=1;
while(i < preorderSize)
{
if(preorder[i] < trav_ptr->val)
{
// printf("less than : %d, preorder[i] : %d\r\n", trav_ptr->val, preorder[i]);
struct TreeNode * new_node = (struct TreeNode *)malloc(sizeof(struct TreeNode));
new_node->val = preorder[i];
new_node->left = NULL;
new_node->right = NULL;
trav_ptr->left = new_node;
push(&tmp_stack, trav_ptr);
trav_ptr = new_node;
}
else
{
printf("more than : %d\r\n", trav_ptr->val);
struct TreeNode * peek_element = NULL;
// printf("tmp_stack.top : %d\r\n",tmp_stack.top);
int j=0;
for(j=tmp_stack.top; j>=0; j--)
{
peek_element = peek(&tmp_stack,j);
if(preorder[i] > peek_element->val)
{
trav_ptr = pop(&tmp_stack);
}
else
{
struct TreeNode * new_node = (struct TreeNode *)malloc(sizeof(struct TreeNode));
new_node->val = preorder[i];
new_node->left = NULL;
new_node->right = NULL;
trav_ptr->right = new_node;
trav_ptr = new_node;
break;
}
}
if(isStackEmpty(&tmp_stack))
{
struct TreeNode * new_node = (struct TreeNode *)malloc(sizeof(struct TreeNode));
new_node->val = preorder[i];
new_node->left = NULL;
new_node->right = NULL;
trav_ptr->right = new_node;
trav_ptr = new_node;
}
}
i++;
}
return root;
}