Design an Expression Tree With Evaluate Function — LeetCode 1628 Python Solution
- Problem
- #1628
- Pattern
- Stack
- Reading time
- 11 min
- Source
- leetcode.com
The problem
Given the postfix tokens of an arithmetic expression, build and return the binary expression tree that represents this expression. Postfix notation is a notation for writing arithmetic expressions in which the operands (numbers) appear before their operators.
Example
- Input
- s = ["3","4","+","2","*","7","/"]
- Output
- 2
- Explanation
- this expression evaluates to the above binary tree with expression ((3+4)*2)/7) = 14/7 = 2.
Python solution
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 here
def evaluate(self) -> int:
pass
class MyNode(Node):
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def evaluate(self) -> int:
x = self.val
if x.isdigit():
return int(x)
left, right = self.left.evaluate(), self.right.evaluate()
if x == '+':
return left + right
if x == '-':
return left - right
if x == '*':
return left * right
if x == '/':
return left // right
"""
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':
stk = []
for s in postfix:
node = MyNode(s)
if not s.isdigit():
node.right = stk.pop()
node.left = stk.pop()
stk.append(node)
return stk[-1]
"""
Your TreeBuilder object will be instantiated and called as such:
obj = TreeBuilder();
expTree = obj.buildTree(postfix);
ans = expTree.evaluate();
"""Complexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(n) auxiliary |
Pattern: Stack
When the most recent unresolved thing is the one that matters, use a stack. LeetCode 1628. Design an Expression Tree With Evaluate Function is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Stack.
The stack guide has the Python template for the pattern and the 194 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1628. Design an Expression Tree With Evaluate Function?
- LeetCode 1628. Design an Expression Tree With Evaluate Function is rated Medium on LeetCode.
- What is the time complexity of LeetCode 1628. Design an Expression Tree With Evaluate Function?
- The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 1628. Design an Expression Tree With Evaluate Function?
- The Python solution on this page uses O(n) auxiliary space.
- What topics does LeetCode 1628. Design an Expression Tree With Evaluate Function cover?
- LeetCode 1628. Design an Expression Tree With Evaluate Function is tagged Stack, Tree, Design, Array, Math and Binary Tree on LeetCode.
- Is LeetCode 1628. Design an Expression Tree With Evaluate Function a premium problem?
- Yes. LeetCode 1628. Design an Expression Tree With Evaluate Function is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.