-
Notifications
You must be signed in to change notification settings - Fork 0
/
Expression Tree Build.cpp
61 lines (60 loc) · 1.75 KB
/
Expression Tree Build.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
/**
* Definition of ExpressionTreeNode:
* class ExpressionTreeNode {
* public:
* string symbol;
* ExpressionTreeNode *left, *right;
* ExpressionTreeNode(string symbol) {
* this->symbol = symbol;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param expression: A string array
* @return: The root of expression tree
*/
void popStk(stack<char> &stk, stack<ExpressionTreeNode*> &nodes, int b, int a[128]) {
while(!stk.empty() && a[stk.top()] >= b) {
ExpressionTreeNode* t = new ExpressionTreeNode(string(1, stk.top()));
t->right = nodes.top();
nodes.pop();
t->left = nodes.top();
nodes.pop();
nodes.push(t);
stk.pop();
}
}
ExpressionTreeNode* build(vector<string> &expression) {
// write your code here
stack<ExpressionTreeNode*> nodes;
stack<char> stk;
int a[128];
a['+'] = a['-'] = 1;
a['*'] = a['/'] = 2;
a['('] = 0;
for(auto &s:expression) {
switch(s.back()) {
case '(':
stk.push('('); break;
case ')':
popStk(stk, nodes,1, a);
stk.pop(); break;
case '+':
case '-':
case '*':
case '/':
popStk(stk, nodes, a[s.back()], a);
stk.push(s.back()); break;
default:
ExpressionTreeNode* t = new ExpressionTreeNode(s);
nodes.push(t);
}
}
popStk(stk, nodes, 1, a);
if(nodes.empty()) return NULL;
return nodes.top();
}
};