367 · Expression Tree Build
Algorithms
Hard
Accepted Rate
29%
Description
Solution
Notes
Discuss
Leaderboard
Record
Description
The structure of Expression Tree is a binary tree to evaluate certain expressions.
All leaves of the Expression Tree have an number string value. All non-leaves of the Expression Tree have an operator string value.
Now, given an expression array, build the expression tree of this expression, return the root of this expression tree.
See wiki:
Expression Tree
Example
Example 1:
Input: [“2”,"“,“6”,”-“,”(“,“23”,”+“,“7”,”)“,”/“,”(“,“1”,”+“,“2”,”)"]
Output: {[-],[],[/],[2],[6],[+],[+],#,#,#,#,[23],[7],[1],[2]}
Explanation:
The expression tree will be like
[ - ]
/ \
[ * ] [ / ]
/ \ / \
[ 2 ] [ 6 ] [ + ] [ + ]
/ \ / \
[ 23 ][ 7 ] [ 1 ] [ 2 ] .
After building the tree, you just need to return root node [-]
.
Example 2:
Input: [“10”,“+”,“(”,“3”,“-”,“5”,“)”,“+”,“7”,"“,“7”,”-",“2”]
Output: {[-],[+],[2],[+],[],#,#,[10],[-],[7],[7],#,#,[3],[5]}
Explanation:
The expression tree will be like
[ - ]
/
[ + ] [ 2 ]
/ \
[ + ] [ * ]
/ \ /
[ 10 ] [ - ] [ 7 ] [ 7 ]
/ \
[3] [5]
After building the tree, you just need to return root node [-]
.
Tags
Related Problems
1908
Boolean expression evaluation
Hard
575
Decode String
Medium
368
Expression Evaluation
Hard
解法1:跟LintCode 370 Convert Expression To Reverse Polish Notation 差不多
先将表达式转换成RPN,然后处理RPN的时候,用到stack,遇到±*/的时候生成新的节点并弹栈两次,分别为新节点的右左节点。最后返回最后的那个新节点就是root。
/**
* 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
*/
ExpressionTreeNode* build(vector<string> &expression) {
int len = expression.size();
stack<string> optrStk;
vector<string> RPN;
map<string, int> prio;
prio["+"] = 1;
prio["-"] = 1;
prio["*"] = 2;
prio["/"] = 2;
//prio["("] = 0;
//prio[")"] = 0;
//generate RPN
for (int i = 0; i < len; ++i) {
string expr = expression[i];
if (expr.size() > 1 || (expr[0] >= '0' && expr[0] <= '9')) {
RPN.push_back(expr);
continue;
}
if (expr == "(") {
optrStk.push(expr);
continue;
}
if (expr == ")") {
while (!optrStk.empty() && optrStk.top() != "(") {
RPN.push_back(optrStk.top());
optrStk.pop();
}
optrStk.pop(); //pop the "("
continue;
}
while (!optrStk.empty() && prio[optrStk.top()] >= prio[expr]) {
RPN.push_back(optrStk.top());
optrStk.pop();
}
optrStk.push(expr);
}
//dump the rest in optrStk to RPN
while (!optrStk.empty()) {
RPN.push_back(optrStk.top());
optrStk.pop();
}
//process RPN
ExpressionTreeNode *root;
stack<ExpressionTreeNode *> RPNStk;
len = RPN.size();
if (len == 0) return NULL;
if (len == 1) return new ExpressionTreeNode(RPN[0]);
for (int i = 0; i < len; i++) {
string RPNexpr = RPN[i];
if (RPNexpr.size() > 1 || (RPNexpr[0] >= '0' && RPNexpr[0] <= '9')) {
RPNStk.push(new ExpressionTreeNode(RPNexpr));
} else {
ExpressionTreeNode *rightNode = RPNStk.top(); RPNStk.pop();
ExpressionTreeNode *leftNode = RPNStk.top(); RPNStk.pop();
root = new ExpressionTreeNode(RPNexpr);
root->left = leftNode;
root->right = rightNode;
RPNStk.push(root);
}
}
return root;
}
};
解法2:递归。应该可以每次找到优先级最低的那个节点,然后左右递归。