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

猜您喜欢...

  1. Patterns for breaking down questions you haven
  2. Daily Interview Problem: Merge K Sorted Linked Lists
  3. Sorting a list with 3 unique numbers
  4. Algorithm Interview Question: Symmetric k-ary Tree
  5. Daily Interview Problem: Room scheduling
  6. Windows Scripting
  7. [Daily Problem] Remove k-th Last Element From Linked List
  8. Algorithm Interview: No Adjacent Repeating Characters
  9. Daily Interview Problem: Reconstrunct Binary Tree from Preorder and Inorder Traversals
  10. Kaprekar

评论 (0)

    当前页暂时没有评论。


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