Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
30 lines (27 loc) · 827 Bytes

File metadata and controls

30 lines (27 loc) · 827 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//
// Solution124.cpp
// Algorithm
//
// Created by Pancf on 2020/12/13.
// Copyright © 2020 Pancf. All rights reserved.
//
#include "Solution124.hpp"
#include <limits.h>
#include <algorithm>
int _pathSum(TreeNode *node, int &maxVal)
{
if (node == nullptr) return 0;
// 左子树中包含node->left节点的路径最大值
int maxOfLeft = std::max(_pathSum(node->left, maxVal), 0);
// 右子树中包含node->right节点的路径最大值
int maxOfRight = std::max(_pathSum(node->right, maxVal), 0);
maxVal = std::max(maxVal, maxOfLeft + node->val + maxOfRight);
// 以当前节点为根节点的路径最大值
return std::max(maxOfLeft, maxOfRight) + node->val;
}
int Solution124::maxPathSum(TreeNode *root)
{
int maxVal = INT_MIN;
_pathSum(root, maxVal);
return maxVal;
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.