leetcode【124】Binary Tree Maximum Path Sum【c++,遞歸和非遞歸】
問題描述:
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.
Example 1:
Input: [1,2,3]
1
/ \
2 3
Output: 6
Example 2:
Input: [-10,9,20,null,null,15,7]
-10
/ \
9 20
/ \
15 7
Output: 42
源碼:
樹的問題當然離不開遞歸了。找到以每個點為起點的路徑的最大值。時間86%,空間100%。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int result = INT_MIN;
int change(TreeNode* root){
if(!root) return 0;
int L = max(change(root->left), 0);
int R = max(change(root->right), 0);
result = max(result, root->val+L+R);
return root->val+max(L,R);
}
int maxPathSum(TreeNode* root) {
if(!root) return 0;
change(root);
return result;
}
};
有遞歸自然就有非遞歸,用一個帶make_pair的棧,模擬兩個遞歸的變量可以,但是我們今天只看看后序遍歷的方法:
時間和空間都和遞歸是一樣的。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxPathSum(TreeNode* root) {
if(!root) return 0;
stack<TreeNode*> st;
int result = INT_MIN;
st.push(root);
TreeNode* pre = NULL;
while(!st.empty()){
TreeNode* tmp = st.top();
if((!tmp->left && !tmp->right) || (pre && (pre==tmp->left || pre==tmp->right))){
st.pop();
pre = tmp;
int L, R;
if(tmp->left) L = max(tmp->left->val, 0);
if(tmp->right) R = max(tmp->right->val, 0);
if(tmp->left && tmp->right){
result = max(result, L + R + tmp->val);
tmp->val += max(L, R);
}
else if(tmp->left){
result = max(result, L + tmp->val);
tmp->val += L;
}
else if(tmp->right){
result = max(result, R + tmp->val);
tmp->val += R;
}
else{
result = max(result, tmp->val);
}
}
else{
if(tmp->right) st.push(tmp->right);
if(tmp->left) st.push(tmp->left);
}
}
return result;
}
};
智能推薦
124. Binary Tree Maximum Path Sum
這道題給一個二叉樹,要求最大的一條路徑,這條路徑必須沿著parent-child,但是可以是從任意節點到任意節點的。 遇到樹的問題大多可以用遞歸來做,這個題也是二叉樹的遍歷,所以想到DFS。 遍歷這棵樹,遍歷到節點root的時候,最大的路徑有三種可能: (1)左子樹最大路徑+root (2)右子樹最大路徑+root (3)左子樹最大路徑+root+右子樹最大路徑 (4)root 這里注意兩個問題:...
124*. Binary Tree Maximum Path Sum
題目描述(困難難度) 考慮一條路徑,可以從任意節點開始,每個節點最多經過一次,問經過的節點的和最大是多少。 解法一 遞歸 首先看到二叉樹的題,肯定就是想遞歸了。遞歸常規的思路,肯定是遞歸考慮左子樹的最大值,遞歸考慮右子樹的最大值。 問題就來了,怎么考慮包含根節點的最大路徑等于多少?因為我們遞歸求出來的最大 left 可能不包含根節點的左孩子,例如下邊的情況。 左子樹的最大值 left 肯定就是 4...
124. Binary Tree Maximum Path Sum
題目鏈接:https://leetcode.com/problems/binary-tree-maximum-path-sum/ 代碼 思路詳解 保留每個接點單側最大值 單側值=root.val+max(left,right,0) 當前最大值=max(單側值,root.val+左值+右值)...
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 con...
124. Binary Tree Maximum Path Sum(圖解)
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...
猜你喜歡
freemarker + ItextRender 根據模板生成PDF文件
1. 制作模板 2. 獲取模板,并將所獲取的數據加載生成html文件 2. 生成PDF文件 其中由兩個地方需要注意,都是關于獲取文件路徑的問題,由于項目部署的時候是打包成jar包形式,所以在開發過程中時直接安照傳統的獲取方法沒有一點文件,但是當打包后部署,總是出錯。于是參考網上文章,先將文件讀出來到項目的臨時目錄下,然后再按正常方式加載該臨時文件; 還有一個問題至今沒有解決,就是關于生成PDF文件...
電腦空間不夠了?教你一個小秒招快速清理 Docker 占用的磁盤空間!
Docker 很占用空間,每當我們運行容器、拉取鏡像、部署應用、構建自己的鏡像時,我們的磁盤空間會被大量占用。 如果你也被這個問題所困擾,咱們就一起看一下 Docker 是如何使用磁盤空間的,以及如何回收。 docker 占用的空間可以通過下面的命令查看: TYPE 列出了docker 使用磁盤的 4 種類型: Images:所有鏡像占用的空間,包括拉取下來的鏡像,和本地構建的。 Con...
requests實現全自動PPT模板
http://www.1ppt.com/moban/ 可以免費的下載PPT模板,當然如果要人工一個個下,還是挺麻煩的,我們可以利用requests輕松下載 訪問這個主頁,我們可以看到下面的樣式 點每一個PPT模板的圖片,我們可以進入到詳細的信息頁面,翻到下面,我們可以看到對應的下載地址 點擊這個下載的按鈕,我們便可以下載對應的PPT壓縮包 那我們就開始做吧 首先,查看網頁的源代碼,我們可以看到每一...