#include "stdafx.h"
/**
- 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, int &val) {
if (root == nullptr) return 0;
int left = maxPathSum(root->left, val);
int right = maxPathSum(root->right, val);
int lmr = root->val + max(0, left) + max(0, right);
int ret = root->val + max(0, max(left, right));
val = max(val, max(lmr, ret));
return ret;
}
int maxPathSum(TreeNode* root)
{
int val = INT_MIN;
maxPathSum(root, val);
return val;
}
};
报错如下:
11.cpp
C:\code\11\11.cpp(16) : error C2061: syntax error : identifier 'TreeNode'
C:\code\11\11.cpp(26) : error C2061: syntax error : identifier 'TreeNode'
C:\code\11\11.cpp(27) : error C2535: 'int __thiscall Solution::maxPathSum(void)' : member function already defined or declared
C:\code\11\11.cpp(16) : see declaration of 'maxPathSum'