简历:The Google Resume: How to Prepare for a Career and Land a Job at Apple, Microsoft, Google, or any Top Tech Company
算法学习书籍:Introduction to Algorithms
编程珠玑:Programming Pearls (2nd Edition)
C++ 学习:The C++ Programming Language, 4th Edition
经典操作系统书籍,龙书:Operating System Concepts
创业:The Start-up of You: Adapt to the Future, Invest in Yourself, and Transform Your Career
Given a binary tree, find the maximum path sum.
The path may start and end at any node in the tree.
For example:
Given the below binary tree,
Given the below binary tree,
1 / \ 2 3
Return
» Solve this problem6
.We need to pay attention on 3 points in this problem:
1. Binary tree
2. It can start and end in any place.
3. The node value can be positive, 0 and negative.
I referred to the solution from Peking2 at MITBBS. The main idea is recursion and DFS.
Analyse the problem, when we are in a root node, how we decided to include the subtree in the maximum path sum? So we need to calculate the max path sum in both subtree. In the meantime, we need to set a variable "max" to store the max path sum we found during the recursion. Pay attention to one thing is, when a root's value is negative, we need to decide include it or discard it.
不错,比我的简洁很多
ReplyDelete这么说来,整棵树可以成为这样的path了么?如果所有节点都是正数的话。
ReplyDelete