页面顶部 Top
文件:  root - text - article - 2020 - 01 - maximum-path-sum-in-binary-tree.txt
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 207 次阅读, 20474 次搜索 | 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.


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, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 207 次阅读, 20474 次搜索 | 116 个单词 定阅此目录的博客

猜您喜欢...

  1. Windows Scripting
  2. Algorithm Interview: Make the Largest Number
  3. Two Tricks of Delphi
  4. Patterns for breaking down questions you haven
  5. Algorithm Interview: Permutations of numbers
  6. Daily Interview Problem: First Missing Positive Integer
  7. [Daily Problem] Move Zeros
  8. Daily Interview Question: Find Cycles in a Graph
  9. Daily Interview Question: Create a Simple Calculator
  10. Fix Brackets

评论 (0)

    当前页暂时没有评论。


最后更新: October 30 2020 14:21:12 | RSS Subscription
牛排怎么做才好吃? | <meta name="机器人" content="不索引, 跟踪" />