文件: root - text - article - 2019 - 12 - arithmetic-binary-tree.txt
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 595 次阅读, 19908 次搜索 | 142 个单词
| 浏览 | 博客存档
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 595 次阅读, 19908 次搜索 | 142 个单词
| 浏览 | 博客存档
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:
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 595 次阅读, 19908 次搜索 | 142 个单词 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
猜您喜欢...
- PHP Unit Tests on VPS Server
- Print a tree level-by-level, with line-breaks
- Daily Interview Problem: Deepest Node in a Binary Tree
- Daily Interview Problem: Room scheduling
- Maximum In A Stack
- Algorithm Interview: Longest Consecutive Sequence
- Algorithm Interview Question: Nth Fibonacci Number
- Daily Interview Problem: Queue Using Two Stacks
- Spreadsheet Column Title
- Top K Frequent words
©2006~2024 牛排过熟 - 0.00743 秒 - 2631.432 KB/s - 39 在线 内存: 493.48 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - 服务器 PHP 版本号: 7.4.33
牛排怎么做才好吃? | <meta name="机器人" content="不索引, 跟踪" />
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - 服务器 PHP 版本号: 7.4.33
评论 (0)
读写完全 - 一般 - 最小 - 表格 - 所有评论 - 统计
当前页暂时没有评论。