To the Top
File:  root - text - article - 2020 - 01 - maximum-path-sum-in-binary-tree.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 208 Views, 20898 Search Bots | 116 Words

Subscribe to Feed Burner | Browse | Archive
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
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 208 Views, 20898 Search Bots | 116 Words Subscribe to Feed Burner

Related Articles

  1. Number of Ways to Climb Stairs
  2. Batch Programming in XP
  3. Algorithm Interview: No Adjacent Repeating Characters
  4. Daily Interview Question: Longest Sequence with Two Unique Numbers
  5. Daily Interview Problem: Validate Binary Search Tree
  6. Daily Interview Problem: 3 Sum
  7. ImageMagick Vulnerabilities -  CVE-2016–3714
  8. Daily Interview Problem: Merge K Sorted Linked Lists
  9. Linode Support Ticket 10029540 - Other - Important Notice Regarding Ubuntu 17.10 Image
  10. Floor and Ceiling of a Binary Search Tree

Comments (0)

Your Email (Domain Part Not Exposed):

Your Comments:

Privately By Mail Colors More Smileys S x y @

Verification (Click Image 2 Refresh):

    Be the first one to comment this page !


Page Edited: October 30 2020 14:21:09 | RSS Subscription
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />