/**
* 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:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
TreeNode* node;
TreeNode* last=NULL;
TreeNode* tmp;
stack<TreeNode*> st;
while(!st.empty()||root){
while(root){
st.push(root);
root=root->left;
}
tmp=st.top();
if(!tmp->right||tmp->right==last){
res.push_back(tmp->val);
last=tmp;
st.pop();
continue;
}
root=tmp->right;
last=NULL;
}
return res;
}
};
/**
* 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 sum=INT_MIN;
// TreeNode* ori;
int pathsum(TreeNode* root){
if(!root) return 0;
else if(!root->left&&!root->right){
if(sum<root->val) sum=root->val;
return root->val>0?root->val:0;
}
int rl=pathsum(root->left);
int rr=pathsum(root->right);
// rl=max(rl,0);
// rr=max(rr,0);
int single=max(rr,rl);
int all=rr+rl;
int cur=all+root->val;
if(cur>sum) sum=cur;
cur=single+root->val;
return cur>0?cur:0;
}
int maxPathSum(TreeNode* root) {
//if(!root) return 0;
// ori=root;
pathsum(root);
return sum;
}
};