leetcode Binary Tree Maximum Path Sum
来源:互联网 发布:unity3d就业前景 知乎 编辑:程序博客网 时间:2024/06/07 07:23
Binary Tree Maximum Path Sum
Total Accepted: 3538 Total Submissions: 19408My SubmissionsGiven a binary tree, find the maximum path sum.
The path may start and end at any node in the tree.
For example:
Given the below binary tree,
1 / \ 2 3
Return 6
.
I'm ashamed of mistakes I made because I misunderstood the meaning of path. The path is a chain instead of a subtree. # stands for Null, the following is the wrong code :
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int max3(int a,int b,int c){ int max1=max(a,b); int max2=max(max1,c); return max2; } int maxPathSum(TreeNode *root) { // Note: The Solution object is instantiated only once and is reused by each test case. if(root==NULL) return 0; int l,r,max1,max2,maxV,v; max1=max2=v=root->val; if(root->left!=NULL){ l=maxPathSum(root->left); max1=max3(l,v,l+v); } if(root->right!=NULL){ r=maxPathSum(root->right); max2=max3(r,v,r+v); } if(root->left!=NULL&&root->right!=NULL) maxV=max3(max1,max2,l+r+v); else maxV=max(max1,max2); return maxV; }};
I changed the code in the following way, whereas the code is still lengthy:
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int max3(int a,int b,int c){ int max1=max(a,b); int max2=max(max1,c); return max2; } int max4(int a,int b,int c,int d){ int max1=max(a,b); int max2=max(c,d); int max3=max(max1,max2); return max3; } int DFS(TreeNode *root,int &res){ int l=0,r=0,max1,max2,maxV,v; max1=max2=v=root->val; if(root->left!=NULL){ l=DFS(root->left,res); max1=max3(l,v,l+v); } if(root->right!=NULL){ r=DFS(root->right,res); max2=max3(r,v,r+v); } if(root->left!=NULL&&root->right!=NULL) res=max4(max1,max2,l+r+v,res); else res=max3(max1,max2,res); return max3(v,v+l,v+r); } int maxPathSum(TreeNode *root) { // Note: The Solution object is instantiated only once and is reused by each test case. int res; if(root==NULL) return 0; else{ res=root->val; DFS(root,res); } return res; }};
I find another concise version from the Internet like that:
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int res=0; int DFS(TreeNode *root){ if(root==NULL) return 0; int l=DFS(root->left); int r=DFS(root->right); int v=root->val; if(l>0) v+=l; if(r>0) v+=r; if(v>res) res=v; return max(root->val+max(l,r),root->val); } int maxPathSum(TreeNode *root) { // Note: The Solution object is instantiated only once and is reused by each test case. if(root==NULL) return 0; res=root->val; DFS(root); return res; }};
- *****(leetcode) Binary Tree Maximum Path Sum (tree)
- LeetCode Binary Tree Maximum Path Sum
- [Leetcode] Binary Tree Maximum Path Sum
- [leetcode] Binary Tree Maximum Path Sum
- LeetCode Binary Tree Maximum Path Sum
- [LeetCode] Binary Tree Maximum Path Sum
- leetcode Binary Tree Maximum Path Sum
- 【leetcode】Binary Tree Maximum Path Sum
- LeetCode - Binary Tree Maximum Path Sum
- [LeetCode]Binary Tree Maximum Path Sum
- [Leetcode]Binary Tree Maximum Path Sum
- Leetcode: Binary Tree Maximum Path Sum
- LeetCode-Binary Tree Maximum Path Sum
- [leetcode] Binary Tree Maximum Path Sum
- LeetCode - Binary Tree Maximum Path Sum
- LeetCode 124: Binary Tree Maximum Path Sum
- 【Leetcode】Binary Tree Maximum Path Sum
- LeetCode | Binary Tree Maximum Path Sum
- 去除inline-block元素间间距的N种方法
- 关于数据库设计中主键问题的思考
- linux下查看软件安装状态
- GetDocument missing storage-class or type specifiers的解决方法
- 百度地图API提供Geocoder类
- leetcode Binary Tree Maximum Path Sum
- 写这个博客的原因
- 查看虚拟机的linux内核版本号
- android键盘遮挡webview的输入框问题
- getHibernateTemplate().find(hql) 用法 和参数为sql的用法
- 如何判断a、b、c三个字段同时为0则不显示这条数据
- Tomcat连接不上数据库原因之一
- Histograms of Sparse Codes for Object Detection
- 有关几种空字符串的区别