Binary Tree Maximum Path Sum
1,題目要求
Given a non-empty binary tree, find the maximum path sum.
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.
給定非空二叉樹,找到最大路徑總和。
對于此問題,路徑定義為沿著父子連接從樹中的某個起始節點到任何節點的任何節點序列。 該路徑必須至少包含一個節點,并且不需要通過根節點。
2,題目思路
對于這道題,求一條樹中的路徑使得加和最大。
一顆樹的路徑,從一個開始節點出發,向上走0步或者k步,到達某一個根節點,然后再向下走0步或者k步。一旦它往下走,就不會再上升。因此,每條路徑都有一個最高節點,其也是這條路徑上其他所有節點的最低公共祖先。
在對這個問題的解決上,利用遞歸是比較方便的策略。
在遞歸的過程中,對于某一個節點,我們會遞歸地計算它的左子樹和右子樹的最大的路徑之和,然后判斷當前節點作為根節點時,路徑的值是否是當前所遍歷到的所有節點的最大值。
之后,我們返回這個節點作為上升或下降路徑的節點的值即可。
3,代碼實現
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
static const auto s = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return nullptr;
}();
class Solution {
public:
int maxPathSum(TreeNode* root) {
int res = INT_MIN;
maxPathHelper(root, res);
return res;
}
private:
int maxPathHelper(TreeNode* node, int &res){
if(node == nullptr)
return 0;
int left = max(0, maxPathHelper(node->left, res));
int right= max(0, maxPathHelper(node->right,res));
res = max(res, left + right + node->val);
return max(left, right) + node->val;
}
};
智能推薦
lintcode94- Binary Tree Maximum Path Sum- medium
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. Example Given the below binary tree: return 6. 總結:二叉樹里起始點和終結點都任意的題目,其實可以考慮成接龍題。其實對于某個節點單...
【重點】LeetCode 124. Binary Tree Maximum Path Sum
LeetCode 124. Binary Tree Maximum Path Sum 參考鏈接:http://zxi.mytechroad.com/blog/tree/leetcode-124-binary-tree-maximum-path-sum/ Solution1: Time complexity O(n)O(n) Space complexity O(h)O(h) 代碼如下: 花花醬講的...
Leetcode 124. Binary Tree Maximum Path Sum
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connection...
LeetCode: 124. Binary Tree Maximum Path Sum
LeetCode: 124. Binary Tree Maximum Path Sum 題目描述 Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the...
猜你喜歡
leetcode 124. Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum 題目描述 找出盡可能使得和最大的路徑(所有點需要連接) 解題思路 設計遞歸函數findPathSum(node),傳入節點,節點為空時停止遞歸,返回到傳入節點(左右子節點)為止積累的最大和。 如圖左半部分,每次計算三個值: sumLeft:根節點和左子樹的和 sumRight:根節點和右子樹的和 sumAll:根節點和左右子樹的和 計算當前...
?LeetCode 124. Binary Tree Maximum Path Sum
文章目錄 題目描述 知識點 實現 碼前思考 代碼實現 碼后反思 題目描述 知識點 樹形DP 實現 碼前思考 看見最優化的問題,我們基本上是要思考一下能不能使用DP來進行求解,雖然可能有時不行,但是一定要先思考能不能用DP,不能沒有嘗試就放棄DP思想; 這其實是一類非常常見的DP思想,為什么說常見呢?因為這個問題可以很好的轉換成另外的問題; 問題轉化 原始的問題其實我們是很難使用動態規劃的,我們需要...
freemarker + ItextRender 根據模板生成PDF文件
1. 制作模板 2. 獲取模板,并將所獲取的數據加載生成html文件 2. 生成PDF文件 其中由兩個地方需要注意,都是關于獲取文件路徑的問題,由于項目部署的時候是打包成jar包形式,所以在開發過程中時直接安照傳統的獲取方法沒有一點文件,但是當打包后部署,總是出錯。于是參考網上文章,先將文件讀出來到項目的臨時目錄下,然后再按正常方式加載該臨時文件; 還有一個問題至今沒有解決,就是關于生成PDF文件...
電腦空間不夠了?教你一個小秒招快速清理 Docker 占用的磁盤空間!
Docker 很占用空間,每當我們運行容器、拉取鏡像、部署應用、構建自己的鏡像時,我們的磁盤空間會被大量占用。 如果你也被這個問題所困擾,咱們就一起看一下 Docker 是如何使用磁盤空間的,以及如何回收。 docker 占用的空間可以通過下面的命令查看: TYPE 列出了docker 使用磁盤的 4 種類型: Images:所有鏡像占用的空間,包括拉取下來的鏡像,和本地構建的。 Con...