文件: root - text - article - 2020 - 01 - maximum-path-sum-in-binary-tree.txt
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 539 次阅读, 28145 次搜索 | 116 个单词
| 浏览 | 博客存档
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 539 次阅读, 28145 次搜索 | 116 个单词
| 浏览 | 博客存档
Hi, here's your problem today. This problem was recently asked by Facebook:
You are given the root of a binary tree. Find the path between 2 nodes that maximizes the sum of all the nodes in the path, and return the sum. The path does not necessarily need to go through the root.
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 539 次阅读, 28145 次搜索 | 116 个单词 You are given the root of a binary tree. Find the path between 2 nodes that maximizes the sum of all the nodes in the path, and return the sum. The path does not necessarily need to go through the root.
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def maxPathSum(root):
# Fill this in.
# (* denotes the max path)
# *10
# / \
# *2 *10
# / \ \
# *20 1 -25
# / \
# 3 4
root = Node(10)
root.left = Node(2)
root.right = Node(10)
root.left.left = Node(20)
root.left.right = Node(1)
root.right.right = Node(-25)
root.right.right.left = Node(3)
root.right.right.right = Node(4)
print maxPathSum(root)
# 42
猜您喜欢...
- Daily Interview Problem:Create a balanced binary search tree
- Skip the readings, focus on problems. And use all the hints!
- Algorithm Interview: Longest Consecutive Sequence
- [Daily Problem] Remove k-th Last Element From Linked List
- Daily Interview Problem: Word Ordering in a Different Alphabetical Order
- Daily Interview Problem: Longest Substring With K Distinct Characters
- Linode Support Ticket 10029540 - Other - Important Notice Regarding Ubuntu 17.10 Image
- How to Play WAV music under DOS?
- Daily Interview Problem: Maximum Profit From Stocks
- Daily Interview Problem: First Missing Positive Integer
©2006~2024 牛排过熟 - 0.01002 秒 - 2000.7 KB/s - 33 在线 内存: 494.29 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)
读写完全 - 一般 - 最小 - 表格 - 所有评论 - 统计
当前页暂时没有评论。