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. LRU

    rsms/js-lru LRU缓存介绍与实现 (Java) 使用场景 缓存计算结果

  2. PAT (Basic Level) Practise:1005. 继续(3n+1)猜想

    [题目链接] 卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对n=3进 ...

  3. PAT (Basic Level) Practise:1039. 到底买不买

    [题目链接] 小红想买些珠子做一串自己喜欢的珠串.卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖.于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有 ...

  4. 自动构建Makefile(1)--C/C++编译流程&Makefile规则简介

      前言: 大家在Windows上使用VS构建C/C++程序时,不需要自己编辑略显晦涩的Makefile文件,而对于初学者而言, 他们甚至没意识到它的存在.VS是自动生成Makefile文件, 并构建 ...

  5. leetcode 146. LRU Cache ----- java

    esign and implement a data structure for Least Recently Used (LRU) cache. It should support the foll ...

  6. POJ 3041 Asteroids(最小点覆盖集)

                                                                      Asteroids Time Limit: 1000MS   Mem ...

  7. install gcc under suse

    SUSE 11中安装GCC开发环境 SUSE11中安装GCC开发环境 安装包下载网站:http://213.174.32.130/sles/distribution/11.0-SP1/repo/dis ...

  8. 黑马程序员——JAVA基础之Map集合

    ------- android培训.java培训.期待与您交流! ---------- Map集合: 该集合存储键值对.一对一对往里存.而且要保证键的唯一性. 和Set很像,其实Set底层就是使用了M ...

  9. dictEntry **table;

    typedef struct dictht { dictEntry **table; PORT_ULONG size; PORT_ULONG sizemask; PORT_ULONG used;} d ...

  10. Articles Every Programmer Must Read

    http://javarevisited.blogspot.sg/2014/05/10-articles-every-programmer-must-read.html Being a Java pr ...