Lesson learnt: for any calculator problems, keep 2 stacks: 1 for operators and 1 for operands.

class Solution
{
stack<ExpressionTreeNode*> op;
stack<ExpressionTreeNode*> data; void eval()
{
ExpressionTreeNode *opnode = op.top(); op.pop();
ExpressionTreeNode *data1 = data.top(); data.pop();
ExpressionTreeNode *data2 = data.top(); data.pop();
opnode->left = data2;
opnode->right = data1;
data.push(opnode);
}
public:
ExpressionTreeNode* build(vector<string> &expression)
{
for(auto &tmp : expression)
{
ExpressionTreeNode *node = new ExpressionTreeNode(tmp); switch(tmp[])
{
case '(':
op.push(node);
break;
case '+':
case '-':
while(!op.empty()&&op.top()->symbol[]!='(')
{
eval();
}
op.push(node);
break;
case '*':
case '/':
while(!op.empty()&&(op.top()->symbol[]=='*'||op.top()->symbol[]=='/'))
{
eval();
}
op.push(node);
break;
case ')':
while(op.top()->symbol[]!='(')
{
eval();
}
op.pop();
break;
default:
data.push(node);
break;
}
}
while(!op.empty())
{
eval();
} if(data.empty()) return nullptr;
return data.top();
}
};

LintCode "Expression Tree Build"的更多相关文章

  1. Expression Tree Build

    The structure of Expression Tree is a binary tree to evaluate certain expressions.All leaves of the ...

  2. [LintCode] Segment Tree Build II 建立线段树之二

    The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...

  3. [LintCode] Segment Tree Build 建立线段树

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  4. Lintcode: Segment Tree Build

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  5. lintcode :Segmemt Tree Build II

    题目 Segmemt Tree Build II The structure of Segment Tree is a binary tree which each node has two attr ...

  6. 【C#表达式树 开篇】 Expression Tree - 动态语言

    .NET 3.5中新增的表达式树(Expression Tree)特性,第一次在.NET平台中引入了"逻辑即数据"的概念.也就是说,我们可以在代码里使用高级语言的形式编写一段逻辑, ...

  7. Expression Tree Basics 表达式树原理

    variable point to code variable expression tree data structure lamda expression anonymous function 原 ...

  8. Expression Tree 扩展MVC中的 HtmlHelper 和 UrlHelper

    表达式树是LINQ To everything 的基础,同时各种类库的Fluent API也 大量使用了Expression Tree.还记得我在不懂expression tree时,各种眼花缭乱的A ...

  9. 使用Expression Tree构建动态LINQ查询

    这篇文章介绍一个有意思的话题,也是经常被人问到的:如何构建动态LINQ查询?所谓动态,主要的意思在于查询的条件可以随机组合,动态添加,而不是固定的写法.这个在很多系统开发过程中是非常有用的. 我这里给 ...

随机推荐

  1. 多层CCLayer的touch冲突解决

    一般通过layer. setTouchPriority()方法来设置 touch优先级,数值越小,优先级越高,但有时多人开发过程中,多层layer叠在一起,无法通过setTouchPrority()来 ...

  2. java 读取文件的常用方式

    1.读取: public class ReadFromFile { /** * 以字节为单位读取文件,常用于读二进制文件,如图片.声音.影像等文件. */ public static void rea ...

  3. 141. Linked List Cycle

    Given a linked list, determine if it has a cycle in it. 代码如下: /** * Definition for singly-linked lis ...

  4. iOS8适配工作

    1 按钮,菜单文字被加上下划线的问题. 2 状态栏被遮挡的问题.(iPhone6明显,iPhone4S无) 3 使用xcode6最新版本进行编译出现的通知无法呈现的问题(也不进行提示) 4 权限检测( ...

  5. ros与下位机通信常用的c++ boost串口应用--22

    摘要: 原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 一.首先移植c++ boost 库: 1. 先去 Boost官网 下载最新的Boost版本, 我 ...

  6. C#部分---函数添加基本格式;

    格式1:没有参数,没有返回值 (无参无返) 添加函数: /// <summary> /// 累加求和的方法,没有参数,没有返回值 /// </summary> public v ...

  7. leetcode 134. Gas Station ----- java

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  8. timus 1136 Parliament(二叉树)

    Parliament Time limit: 1.0 secondMemory limit: 64 MB A new parliament is elected in the state of MMM ...

  9. 【P1203】买花

    我先在已经弱到连高精乘单精都能写错的地步了QAQ 原题: 求一个小于等于N的数M,使得phi(M)/M最小,其中phi(M)是与M互质且比M小的数的个数.例如phi(4)=2,因为1,3和4互质. N ...

  10. Html-双斜杠//开头的URL(依赖协议的URL)

    今天看京东HTTPS,发现链接都是这种 <a href="//chaoshi.jd.com">京东超市</a> URL是以双斜杠"//" ...