File: root - text - article - 2019 - 12 - arithmetic-binary-tree.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 514 Views, 18872 Search Bots | 142 Words
| Browse | Archive
![Subscribe to Feed Burner Subscribe to Feed Burner](/images/form/rss.gif)
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 514 Views, 18872 Search Bots | 142 Words
![Subscribe to Feed Burner Subscribe to Feed Burner](/images/form/rss.gif)
Hi, here's your problem today. This problem was recently asked by Apple:
You are given a binary tree representation of an arithmetic expression. In this tree, each leaf is an integer value,, and a non-leaf node is one of the four operations: '+', '-', '*', or '/'.
Write a function that takes this tree and evaluates the expression.
Example:
This is a representation of the expression (3 + 2) * (4 + 5), and should return 45.
Here's a starting point:
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 514 Views, 18872 Search Bots | 142 Words You are given a binary tree representation of an arithmetic expression. In this tree, each leaf is an integer value,, and a non-leaf node is one of the four operations: '+', '-', '*', or '/'.
Write a function that takes this tree and evaluates the expression.
Example:
*
/ \
+ +
/ \ / \
3 2 4 5
This is a representation of the expression (3 + 2) * (4 + 5), and should return 45.
Here's a starting point:
class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
PLUS = "+"
MINUS = "-"
TIMES = "*"
DIVIDE = "/"
def evaluate(root):
# Fill this in.
tree = Node(TIMES)
tree.left = Node(PLUS)
tree.left.left = Node(3)
tree.left.right = Node(2)
tree.right = Node(PLUS)
tree.right.left = Node(4)
tree.right.right = Node(5)
print evaluate(tree)
# 45
![Subscribe to Feed Burner Subscribe to Feed Burner](/images/form/rss.gif)
Related Articles
- Daily Interview Problem: Merge Overlapping Intervals
- [Daily Problem] Course Prerequisites
- PHP Unit Tests on VPS Server
- A trick for getting good at coding interviews FASTER
- Daily Interview Problem: Reverse Words in a String
- Algorithm Interview: Subarray With Target Sum
- Sort a Partially Sorted List
- Plus One
- Daily Interview Question: Edit Distance
- CVE-2015-8874 - cPanel EasyApache Vulnerabilities
©2006~2024 SteakOverCooked - 0.01215 Seconds(s) - 1582.996 KB/s - 10 Online
Memory: 492.38 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />
![](/images/top.jpg)
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !