System Design。使用stack存入数字,当遇到操作符号时,弹出stack中的数字。先出的为right,后出为left。完成操作后再压入stack。操作过程写在tree的evaluate函数里。

import abc
from abc import ABC, abstractmethod
"""
This is the interface for the expression tree Node.
You should not remove it, and you can define some classes to implement it.
"""class Node(ABC):@abstractmethod# define your fields heredef evaluate(self) -> int:passclass postfixtree:def __init__(self, val):self.val = valself.left = Noneself.right = Nonedef evaluate(self):if not self.left and not self.right:return self.valelif self.val == '+':return self.left.evaluate() + self.right.evaluate()elif self.val == '-':return self.left.evaluate() - self.right.evaluate()elif self.val == '*':return self.left.evaluate() * self.right.evaluate()elif self.val == '/':return self.left.evaluate() // self.right.evaluate()
"""
This is the TreeBuilder class.
You can treat it as the driver code that takes the postinfix input
and returns the expression tree represnting it as a Node.
"""class TreeBuilder(object):def buildTree(self, postfix: List[str]) -> 'Node':stack = []for index, char in enumerate(postfix):if char.isdigit():node = postfixtree(int(char))stack.append(node)else:right = stack.pop()left = stack.pop()node = postfixtree(char)node.left = leftnode.right = rightstack.append(node)return stack[-1]"""
Your TreeBuilder object will be instantiated and called as such:
obj = TreeBuilder();
expTree = obj.buildTree(postfix);
ans = expTree.evaluate();
"""

Leetcode 1628. Design an Expression Tree With Evaluate Function [Python]相关推荐

  1. 【Leetcode】1628. Design an Expression Tree With Evaluate Function

    题目地址: https://leetcode.com/problems/design-an-expression-tree-with-evaluate-function/ 给定一个后缀表达式,实现建立 ...

  2. Expression Tree 上手指南 (二)

    上回我们说到Expression Tree是一种表示编程语言中"表达式"概念的树状数据结构,并且学习了从Lambda表达式自动生成表达式树的C#语法.那么它到底有什么用呢?其实上一 ...

  3. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  4. 反射,Expression Tree,IL Emit 属性操作对比

    .net的反射(Reflection) 是.Net中获取运行时类型信息的一种方法,通过反射编码的方式可以获得 程序集,模块,类型,元数据等信息. 反射的优点在于微软提供的API调用简单,使用方便: 表 ...

  5. leetcode 622. Design Circular Queue | 641. 设计循环双端队列(Java)

    题目 https://leetcode.com/problems/design-circular-deque/ 题解 相关问题:leetcode 622. Design Circular Queue ...

  6. leetcode 211. Design Add and Search Words Data Structure | 211. 添加与搜索单词 - 数据结构设计(Java)

    题目 https://leetcode.com/problems/design-add-and-search-words-data-structure/ 题解 字典树 + 深度优先搜素,思路参考 le ...

  7. 乘风破浪:LeetCode真题_010_Regular Expression Matching

    乘风破浪:LeetCode真题_010_Regular Expression Matching 一.前言 关于正则表达式我们使用得非常多,但是如果让我们自己写一个,却是有非常大的困难的,我们可能想到状 ...

  8. [转]打造自己的LINQ Provider(上):Expression Tree揭秘

    概述 在.NET Framework 3.5中提供了LINQ 支持后,LINQ就以其强大而优雅的编程方式赢得了开发人员的喜爱,而各种LINQ Provider更是满天飞,如LINQ to NHiber ...

  9. 快速上手Expression Tree(一):做一做装配脑袋的Expression Tree 习题

    装配脑袋的习题在这里:Expression Tree上手指南 (一) 不了解Expression Tree的同学可以去看下,很好,很强大. 1: -a 2: a + b * 2 我把这些问题都弄成了方 ...

最新文章

  1. 机器学习之XGBoosting
  2. web.config文件
  3. VC++ 用ADO连接数据库的简单方法
  4. mongoose查询不到数据表中的数据的问题
  5. c语言入门经典案例,c语言入门经典案例及飞源代码.doc
  6. oracle导出命令位置,ORACLE 导出导入命令说明
  7. 3 配置ftp文件服务器,03-FTP和TFTP配置
  8. 介绍一下Python中webbrowser的用法?
  9. postman测试websocket_还在手工写接口测试文档,已经out了
  10. Windows批处理命令
  11. winrar 命令行参数使用
  12. Stata | 导入导出文件
  13. pytorch读取lmdb文件报错,lmdb.InvalidParameterError:解决
  14. 【工具】多人在线协同编辑文档软件
  15. 个人任务五——事后诸葛亮项目回顾
  16. Netty Websocket多人多房间聊天室Demo
  17. mcinabox运行库下载_MCinaBox运行库下载
  18. CUDA中的数学函数
  19. AppScan安全扫描工具-IBM Security App Scan Standard
  20. this is related to npm not being able to find a file

热门文章

  1. Xcode自带的超好用的诊断工具
  2. 怎样进行https证书检查
  3. arcgis lisence manager 无法启动
  4. bp神经网络推导以及物理意义
  5. 【翻译】Visual Place Recognition_ A Survey视觉场景识别综述【四】
  6. linux切换ip地址脚本,批处理实现的ip地址切换的复杂脚本
  7. MarkDown生成目录索引
  8. 全网最全-超大模型+分布式训练架构和经典论文
  9. html表格打印填充分页,window.print()页面打印之表格内容分页填充进行分页打印
  10. Android imageview 双击放大缩小手势放大缩小自由滑动