Problem
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.
ClarificationSee wiki:
Expression Tree
For the expression (2*6-(23+7)/(1+2)) (which can be represented by ["2" "*" "6" "-" "(" "23" "+" "7" ")" "/" "(" "1" "+" "2" ")"]).
The expression tree will be like
[ - ] / [ * ] [ / ] / / [ 2 ] [ 6 ] [ + ] [ + ] / / [ 23 ][ 7 ] [ 1 ] [ 2 ] .
After building the tree, you just need to return root node [-].
Note Solutionclass TreeNode { public int val; public String s; public ExpressionTreeNode root; public TreeNode(int val, String ss) { this.val = val; this.root = new ExpressionTreeNode(ss); } } public class Solution { int get(String a, Integer base) { if (a.equals("+") || a.equals("-")) return 1 + base; if (a.equals("*") || a.equals("/")) return 2 + base; return Integer.MAX_VALUE; } public ExpressionTreeNode build(String[] expression) { // write your code here Stackstack = new Stack (); TreeNode root = null; int val = 0; Integer base = 0; for (int i = 0; i <= expression.length; i++) { if(i != expression.length) { if (expression[i].equals("(")) { base += 10; continue; } if (expression[i].equals(")")) { base -= 10; continue; } val = get(expression[i], base); } TreeNode right = i == expression.length ? new TreeNode( Integer.MIN_VALUE, "") : new TreeNode(val, expression[i]); while (!stack.isEmpty()) { if (right.val <= stack.peek().val) { TreeNode nodeNow = stack.pop(); if (stack.isEmpty()) { right.root.left = nodeNow.root; } else { TreeNode left = stack.peek(); if (left.val < right.val) { right.root.left = nodeNow.root; } else { left.root.right = nodeNow.root; } } } else { break; } } stack.push(right); } return stack.peek().root.left; } };
class Solution { public: /** * @param expression: A string array * @return: The root of expression tree */ int getLevel(string opt) { if (opt == "(") return 0; if (opt == "+" || opt == "-") return 1; if (opt == "*" || opt == "/") return 2; return 3; } bool isOperator(string c) { return (c == "+" || c == "-" || c == "*" || c == "/"); } vectorconvertToRPN(vector &expression) { stack st; vector RPN; int len = expression.size(); for (int i = 0; i < len; ++i) { string c = expression[i]; if (c == "(") st.push(c); else if (c == ")") { while (st.top() != "(") { RPN.push_back(st.top()); st.pop(); } st.pop(); } else { if (!isOperator(c)) st.push(c); else { while (!st.empty() && getLevel(st.top()) >= getLevel(c)) { RPN.push_back(st.top()); st.pop(); } st.push(c); } } } while (! st.empty()) { RPN.push_back(st.top()); st.pop(); } return RPN; } ExpressionTreeNode* build(vector &expression) { // write your code here vector RPN = convertToRPN(expression); int len = RPN.size(); stack nodeStack; for (int i = 0; i < len; ++i) { string s = RPN[i]; ExpressionTreeNode *pNode = new ExpressionTreeNode(s); if (s == "+" || s == "-" || s == "*" || s == "/") { ExpressionTreeNode *pRight = nodeStack.top(); nodeStack.pop(); ExpressionTreeNode *pLeft = nodeStack.top(); nodeStack.pop(); pNode->right = pRight; pNode->left = pLeft; nodeStack.push(pNode); } else nodeStack.push(pNode); } if (nodeStack.empty()) return NULL; else return nodeStack.top(); } };
public class Solution { public boolean isOperator(String s) { return s == "+" || s == "-" || s == "*" || s == "/"; } public int getLevel(String s) { if (s == "(") return 0; if (s == "+" || s == "-") return 1; if (s == "*" || s == "/") return 2; return 3; } public ArrayListconvert(String[] expression) { Stack stack = new Stack<>(); ArrayList deq = new ArrayList<>(); int len = expression.length; for (int i = 0; i < len; ++i) { String s = expression[i]; if (s == "(") stack.push(s); else if (s == ")") { while (stack.peek() != "(") { deq.add(stack.peek()); stack.pop(); } stack.pop();//delete "(" } else { if (!isOperator(s)) { stack.push(s); } else { while (!stack.isEmpty() && getLevel(stack.peek()) >= getLevel(s)) { deq.add(stack.peek()); stack.pop(); } stack.push(s); } } } while (!stack.isEmpty()) { deq.add(stack.peek()); stack.pop(); } return deq; } public ExpressionTreeNode build(String[] expression) { ArrayList deq = convert(expression); System.out.println(deq); int len = deq.size(); Stack stack = new Stack<>(); for (int i = 0; i < len; ++i) { String s = deq.get(i); ExpressionTreeNode node = new ExpressionTreeNode(s); if (s == "+" || s == "-" || s == "*" || s == "/") { ExpressionTreeNode nodeRight = stack.peek(); stack.pop(); ExpressionTreeNode nodeLeft = stack.peek(); stack.pop(); node.right = nodeRight; node.left = nodeLeft; stack.push(node); } else stack.push(node); } if (stack.isEmpty()) return null; else return stack.peek(); } }
import java.util.*; public class Solution { public ExpressionTreeNode build(String[] expression) { Stackop = new Stack (); Stack data = new Stack (); for(int i=0;i ="0")){ //System.out.println("get op "+ tmp); switch(firstc){ case "(": ExpressionTreeNode node = new ExpressionTreeNode(tmp); op.push(node); break; case "+": case "-": while(!op.isEmpty()&&op.peek().symbol.charAt(0)!="("){ ExpressionTreeNode opnode = op.pop(); ExpressionTreeNode data1 = data.pop(); ExpressionTreeNode data2 = data.pop(); opnode.left = data2; opnode.right = data1; data.push(opnode); } ExpressionTreeNode node2 = new ExpressionTreeNode(tmp); op.push(node2); break; case "*": case "/": while(!op.isEmpty()&&(op.peek().symbol.charAt(0)=="*"||op.peek().symbol.charAt(0)=="/")){ ExpressionTreeNode opnode = op.pop(); ExpressionTreeNode data1 = data.pop(); ExpressionTreeNode data2 = data.pop(); opnode.left = data2; opnode.right = data1; data.push(opnode); } ExpressionTreeNode node3 = new ExpressionTreeNode(tmp); op.push(node3); break; case ")": while(op.peek().symbol.charAt(0)!="("){ ExpressionTreeNode opnode = op.pop(); ExpressionTreeNode data1 = data.pop(); ExpressionTreeNode data2 = data.pop(); opnode.left = data2; opnode.right = data1; data.push(opnode); } op.pop(); } }else{ //System.out.println("add data "+tmp); ExpressionTreeNode data1 = new ExpressionTreeNode(tmp); data.push(data1); } } while(!op.isEmpty()){ ExpressionTreeNode opnode = op.pop(); ExpressionTreeNode data1 = data.pop(); ExpressionTreeNode data2 = data.pop(); opnode.left = data2; opnode.right = data1; data.push(opnode); } if(data.isEmpty()) return null; return data.pop(); } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/65024.html
摘要:唯一需要注意的就是的赋值取左右子树的的较大值,最后一层的独立结点的为对应数组中的值。 Segment Tree Build I Problem The structure of Segment Tree is a binary tree which each node has two attributes start and end denote an segment / interv...
摘要:题目是要查询到这个区间内某一点的。值是从最底层的子节点值里取最大值。因此,不用太复杂,递归就可以了。与所不同的是,对所给区间内的元素个数求和,而非筛选。这样就会出现的情况,视作本身处理。 Segment Tree Query Problem For an integer array (index from 0 to n-1, where n is the size of this ar...
摘要:这道题目是筛选出数组中的最小值,存入新数组。因此,联想到和系列的题目,对于的处理,使用线段树是非常有效的方法。之前我们创建的线段树,有和两个。参照这个参数,可以考虑在这道题增加一个的参数,代表每个结点的最小值。 Problem Given an integer array (index from 0 to n-1, where n is the size of this array),...
摘要:直接的方法不可取因为是每一层。层直接从取出实际上是将这个后应该得到。这个时候考虑逆向,建立一个,将出来的东西再一个顺序,逆逆得顺是一个很好用的操作符,判断一个对象是否是一个类的实例。坑小心一点这种情况啊代码 这道题真是超级棒的stack DFS样板题啊,在这里给自己写个小小的总结 思路:想到stack并不难,这种嵌套式一般是DFS的思想,先走到最里面最小的那个括号,然后逐渐回到上一层→...
阅读 2888·2023-04-26 02:22
阅读 2219·2021-11-17 09:33
阅读 3096·2021-09-22 16:06
阅读 1001·2021-09-22 15:54
阅读 3464·2019-08-29 13:44
阅读 1837·2019-08-29 12:37
阅读 1253·2019-08-26 14:04
阅读 1853·2019-08-26 11:57