forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-binary-expression-tree-from-infix-expression.cpp
49 lines (46 loc) · 1.67 KB
/
build-binary-expression-tree-from-infix-expression.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
// Time: O(n)
// Space: O(n)
// Support +, -, *, /.
class Solution {
public:
Node* expTree(string s) {
static const unordered_map<char, int> precedence = {{'+', 0}, {'-', 0}, {'*', 1}, {'/', 1}};
stack<Node *> operands;
stack<char> operators;
int64_t operand = 0;
for (int i = 0; i < size(s); ++i) {
if (isdigit(s[i])) {
operand = operand * 10 + s[i] - '0';
if (i + 1 == size(s) || !isdigit(s[i + 1])) {
operands.emplace(new Node(operand + '0'));
operand = 0;
}
} else if (s[i] == '(') {
operators.emplace(s[i]);
} else if (s[i] == ')') {
while (!empty(operators) && operators.top() != '(') {
compute(&operands, &operators);
}
operators.pop();
} else if (precedence.count(s[i])) {
while (!empty(operators) && precedence.count(operators.top()) &&
precedence.at(operators.top()) >= precedence.at(s[i])) {
compute(&operands, &operators);
}
operators.emplace(s[i]);
}
}
while (!empty(operators)) {
compute(&operands, &operators);
}
return operands.top();
}
private:
template<typename T>
void compute(stack<T> *operands, stack<char> *operators) {
const auto right = operands->top(); operands->pop();
const auto left = operands->top(); operands->pop();
const char op = operators->top(); operators->pop();
operands->emplace(new Node(op, left, right));
}
};