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(); } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/65024.html
摘要:唯一需要注意的就是的賦值取左右子樹(shù)的的較大值,最后一層的獨(dú)立結(jié)點(diǎn)的為對(duì)應(yīng)數(shù)組中的值。 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...
摘要:題目是要查詢(xún)到這個(gè)區(qū)間內(nèi)某一點(diǎn)的。值是從最底層的子節(jié)點(diǎn)值里取最大值。因此,不用太復(fù)雜,遞歸就可以了。與所不同的是,對(duì)所給區(qū)間內(nèi)的元素個(gè)數(shù)求和,而非篩選。這樣就會(huì)出現(xiàn)的情況,視作本身處理。 Segment Tree Query Problem For an integer array (index from 0 to n-1, where n is the size of this ar...
摘要:這道題目是篩選出數(shù)組中的最小值,存入新數(shù)組。因此,聯(lián)想到和系列的題目,對(duì)于的處理,使用線(xiàn)段樹(shù)是非常有效的方法。之前我們創(chuàng)建的線(xiàn)段樹(shù),有和兩個(gè)。參照這個(gè)參數(shù),可以考慮在這道題增加一個(gè)的參數(shù),代表每個(gè)結(jié)點(diǎn)的最小值。 Problem Given an integer array (index from 0 to n-1, where n is the size of this array),...
摘要:直接的方法不可取因?yàn)槭敲恳粚印又苯訌娜〕鰧?shí)際上是將這個(gè)后應(yīng)該得到。這個(gè)時(shí)候考慮逆向,建立一個(gè),將出來(lái)的東西再一個(gè)順序,逆逆得順是一個(gè)很好用的操作符,判斷一個(gè)對(duì)象是否是一個(gè)類(lèi)的實(shí)例??有⌒囊稽c(diǎn)這種情況啊代碼 這道題真是超級(jí)棒的stack DFS樣板題啊,在這里給自己寫(xiě)個(gè)小小的總結(jié) 思路:想到stack并不難,這種嵌套式一般是DFS的思想,先走到最里面最小的那個(gè)括號(hào),然后逐漸回到上一層→...
摘要:將表達(dá)式轉(zhuǎn)換為逆波蘭式,然后求值轉(zhuǎn)換中綴表達(dá)式為逆波蘭式魯棒性檢測(cè),表達(dá)式中沒(méi)有操作數(shù)計(jì)算逆波蘭式值參考 表達(dá)式類(lèi)算法題小結(jié) [TOC] 聲明 文章均為本人技術(shù)筆記,轉(zhuǎn)載請(qǐng)注明出處:[1] https://segmentfault.com/u/yzwall[2] blog.csdn.net/j_dark/ 表達(dá)式分類(lèi) 根據(jù)運(yùn)算符與相關(guān)操作操作數(shù)的位置不同,將表達(dá)式分為前綴,中綴和后綴表...
閱讀 2938·2023-04-26 02:22
閱讀 2292·2021-11-17 09:33
閱讀 3144·2021-09-22 16:06
閱讀 1079·2021-09-22 15:54
閱讀 3541·2019-08-29 13:44
閱讀 1921·2019-08-29 12:37
閱讀 1327·2019-08-26 14:04
閱讀 1920·2019-08-26 11:57