文件: root - text - article - 2020 - 01 - maximum-path-sum-in-binary-tree.txt
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 526 次阅读, 27709 次搜索 | 116 个单词
| 浏览 | 博客存档
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 526 次阅读, 27709 次搜索 | 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, | 英文 | 主页 | 类别: 计算机科学 | 526 次阅读, 27709 次搜索 | 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 Question: Edit Distance
- Daily Interview Problem: Reconstrunct Binary Tree from Preorder and Inorder Traversals
- Daily Interview Problem: Longest Substring With K Distinct Characters
- [Daily Problem] Course Prerequisites
- ImageMagick Vulnerabilities - CVE-2016–3714
- Windows Scripting
- Daily Interview Problem: Merge Overlapping Intervals
- Batch Programming in XP
- [Daily Problem] Angles of a Clock
- Multitasking
©2006~2024 牛排过熟 - 0.0081 秒 - 2410.286 KB/s - 37 在线 内存: 492.75 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)
读写完全 - 一般 - 最小 - 表格 - 所有评论 - 统计
当前页暂时没有评论。