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 | 526 Views, 27725 Search Bots | 116 Words
| Browse | Archive
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 526 Views, 27725 Search Bots | 116 Words
| 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.
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 526 Views, 27725 Search Bots | 116 Words 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
Related Articles
- Maximum In A Stack
- Algorithm Interview Question: Find the Single Element in an Array of Duplicates
- Top K Frequent words
- Daily Interview Problem: Sort Colors
- Daily Interview Problem: Group Words that are Anagrams
- Daily Interview Problem: Largest BST in a Binary Tree
- How to Play WAV music under DOS?
- Most Frequent Subtree Sum
- Kaprekar
- Daily Interview Problem: Decode String
©2006~2024 SteakOverCooked - 0.00838 Seconds(s) - 2359.783 KB/s - 24 Online Memory: 492.03 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Comments (0)
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !