-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathternary_expression_to_binary_tree.cpp
45 lines (41 loc) · 1.09 KB
/
ternary_expression_to_binary_tree.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
/*
Given a string that contains ternary expressions. The expressions may be nested,task is convert the given ternary expression
to a binary Tree and return the root.
https://practice.geeksforgeeks.org/problems/convert-ternary-expression-to-binary-tree/1
*/
//simple solution using stack //
Node *convertExpression(string str,int i)
{
int n=str.length();
stack<Node*>s;
Node*temp1=(Node*)malloc(sizeof(Node));
temp1->data=str[0];
temp1->left=NULL;
temp1->right=NULL;
s.push(temp1);
Node*root=temp1;
for(int i=1;i<n;i++){
if(str[i-1]=='?'){
Node*temp=(Node*)malloc(sizeof(Node));
temp->data=str[i];
temp->left=NULL;
temp->right=NULL;
temp1=s.top();
temp1->left=temp;
s.push(temp);
}
else if(str[i-1]==':'){
Node *temp=(Node*)malloc(sizeof(Node));
temp->data=str[i];
temp->left=NULL;
temp->right=NULL;
s.pop();
temp1=s.top();
temp1->right=temp;
s.pop();
s.push(temp);
}
}
return root;
}
//you can also do it using recursion but i have not done that (i was stuck:p)