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.

 
Clarification

See wiki:
Expression Tree

Example

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 [-].

分析:

先把expression 转成RPN,然后遇到operator,直接建立一颗树,数的root是operator, 左右节点从stack里面pop就可以,然后把该root压栈。

 /**
* Definition of ExpressionTreeNode:
* public class ExpressionTreeNode {
* public String symbol;
* public ExpressionTreeNode left, right;
* public ExpressionTreeNode(String symbol) {
* this.symbol = symbol;
* this.left = this.right = null;
* }
* }
*/ /**
* Definition of ExpressionTreeNode:
* public class ExpressionTreeNode {
* public String symbol;
* public ExpressionTreeNode left, right;
* public ExpressionTreeNode(String symbol) {
* this.symbol = symbol;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/**
* @param expression: A string array
* @return: The root of expression tree
*/
public ExpressionTreeNode build(String[] expression) { ArrayList<String> RPN = convertToRPN(expression);
if (RPN == null || RPN.size() == ) return null; Stack<ExpressionTreeNode> stack = new Stack<ExpressionTreeNode>();
for (String str : RPN) {
if (isOperator(str)) {
ExpressionTreeNode opnode = new ExpressionTreeNode(str);
ExpressionTreeNode data1 = stack.pop();
ExpressionTreeNode data2 = stack.pop();
opnode.left = data2;
opnode.right = data1;
stack.push(opnode);
} else {
stack.push(new ExpressionTreeNode(str));
}
}
return stack.pop();
} public ArrayList<String> convertToRPN(String[] expression) {
ArrayList<String> list = new ArrayList<String>();
Stack<String> stack = new Stack<String>(); for (int i = ; i < expression.length; i++) {
String str = expression[i];
if (isOperator(str)) {
if (str.equals("(")) {
stack.push(str);
} else if (str.equals(")")) {
while (!stack.isEmpty() && !stack.peek().equals("(")) {
list.add(stack.pop());
}
stack.pop();
} else {
while (!stack.isEmpty() && order(str) <= order(stack.peek())) {
list.add(stack.pop());
}
stack.push(str);
}
} else {
list.add(str);
}
}
while (!stack.isEmpty()) {
list.add(stack.pop());
}
return list;
} private boolean isOperator(String str) {
if (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/") || str.equals("(")
|| str.equals(")")) {
return true;
}
return false;
} private int order(String a) {
if (a.equals("*") || a.equals("/")) {
return ;
} else if (a.equals("+") || a.equals("-")) {
return ;
} else {
return ;
}
}
}

Expression Tree Build的更多相关文章

  1. LintCode "Expression Tree Build"

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

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

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

  3. Expression Tree Basics 表达式树原理

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

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

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

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

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

  6. Reflection和Expression Tree解析泛型集合快速定制特殊格式的Json

    很多项目都会用到Json,而且大部分的Json都是格式固定,功能强大,转换简单等,标准的key,value集合字符串:直接JsonConvert.SerializeObject(List<T&g ...

  7. .NET Expression Tree

    Expression Tree 第一个简单的例子. [TestMethod] public void GodTest() { Expression<Func<int, int, int&g ...

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

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

  9. Segment Tree Build I & II

    Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...

随机推荐

  1. Python之路3【知识点】白话Python编码和文件操作(截载)

    无意发现这篇文章讲的比较好,存下来供参考: http://www.cnblogs.com/luotianshuai/p/5735051.html

  2. [转贴]CentOS7.5 Kubernetes V1.13(最新版)二进制部署集群

    CentOS7.5 Kubernetes V1.13(最新版)二进制部署集群 http://blog.51cto.com/10880347/2326146   一.概述 kubernetes 1.13 ...

  3. QEMU简单知识 以及磁盘格式转换的简单命令

    From 百度百科 QEMU,是由 Fabrice Bellard开发的通用.开源机器模拟与虚拟化软件,Fabrice Bellard是另一个著名的C编译器的作者.QEMU可以在不同的机器上运行独自开 ...

  4. helm 替换源的方法

    网上找了一个 helm 替换源的方法 挺好用的 mark 一下 helm repo remove stable helm repo add stable https://kubernetes.oss- ...

  5. 使用alpine的docker镜像下 dind 的方式安装dotnet core 的一个非dockerfile的方法

    1. 下载dind的镜像 docker pull docker:dind 2. 执行该镜像 docker run -it --privileged --name some-docker -d dock ...

  6. Docker in Docker的安装 路不通

    1. 先启动centos 镜像 然后 再docker cp文件 然后再执行安装报错 [root@CentOS75 ~]# docker run -it centos /bin/bash [root@1 ...

  7. Java之序列化和反序列化

    序列化的对象: package test_demo.SerializableOper; import java.io.Serializable; /* * 序列化对象需要实现序列号接口 * */ pu ...

  8. 5G的作业- 云计算

    作业命题:5G对于保险行业的影响,技术层面和业务模式层面 一.5G网络的特点: 5G网络主要有三大特点,极高的速率 enhanced mobile broadband (eMBB),极大的容量 Mas ...

  9. BZOJ5467 PKUWC2018Slay the Spire(动态规划)

    即求所有情况的最大伤害之和.容易发现应该先打强化牌,至少打一张攻击牌.同样显然的是强化牌和攻击牌都应该按从大到小的顺序打.进一步可以发现,只要还有强化牌,就应该使用(当然至少留一次攻击的机会). 于是 ...

  10. 【hdu1542】线段树求矩形面积并

    分割线内容转载自http://hzwer.com/879.html ------------------------------------------------------------------ ...