LeetCode 124:Binary Tree Maximum Path Sum
来源:互联网 发布:杭州淘宝美工培训 编辑:程序博客网 时间:2024/05/17 02:44
Given a 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 does not need to go through the root.
For example:
Given the below binary tree,
1 / \ 2 3
Return 6
.
Subscribe to see which companies asked this question
//找出二叉树任意一点到另一点的路径,使得和最大.//解题思路:后序遍历,先计算左右子树的值l和r,若l<0或r<0,则不用加上class Solution {public:int maxSum;int maxPathSum(TreeNode* root) {if (root == NULL) return 0;maxSum =root->val;dfs(root);return maxSum;}int dfs(TreeNode* root){if (root == NULL)return 0;int l = dfs(root->left);int r = dfs(root->right);int sum = root->val;if (l > 0) sum = sum + l;if (r > 0) sum = sum + r;maxSum = max(maxSum, sum);return max(root->val, max(l + root->val, r + root->val));}};
#include<iostream> #include<new> #include<vector> #include<algorithm>using namespace std;//Definition for binary tree struct TreeNode{int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};//找出二叉树任意一点到另一点的路径,使得和最大.//解题思路:后序遍历,先计算左右子树的值l和r,若l<0或r<0,则不用加上class Solution {public:int maxSum;int maxPathSum(TreeNode* root) {if (root == NULL) return 0;maxSum =root->val;dfs(root);return maxSum;}int dfs(TreeNode* root){if (root == NULL)return 0;int l = dfs(root->left);int r = dfs(root->right);int sum = root->val;if (l > 0) sum = sum + l;if (r > 0) sum = sum + r;maxSum = max(maxSum, sum);return max(root->val, max(l + root->val, r + root->val));}};void createTree(TreeNode *&root){int i;cin >> i;if (i != 0){root = new TreeNode(i);if (root == NULL)return;createTree(root->left);createTree(root->right);}}int main(){Solution s;TreeNode *root;createTree(root);int sum = s.maxPathSum(root);cout << sum << endl;system("pause");return 0;}
1 0
- LeetCode 124: Binary Tree Maximum Path Sum
- LeetCode 124 Binary Tree Maximum Path Sum
- LeetCode: Binary Tree Maximum Path Sum [124]
- LeetCode 124 Binary Tree Maximum Path Sum
- LeetCode(124) Binary Tree Maximum Path Sum
- [LeetCode 124]Binary Tree Maximum Path Sum
- leetcode || 124、Binary Tree Maximum Path Sum
- LeetCode #124 Binary Tree Maximum Path Sum
- leetcode 124: Binary Tree Maximum Path Sum
- 124 Binary Tree Maximum Path Sum [Leetcode]
- LeetCode 124: Binary Tree Maximum Path Sum
- LeetCode 124:Binary Tree Maximum Path Sum
- LeetCode 124 Binary Tree Maximum Path Sum
- Leetcode 124 Binary Tree Maximum Path Sum
- Leetcode #124 Binary Tree Maximum Path Sum
- Leetcode-124: Binary Tree Maximum Path Sum
- *****(leetcode) Binary Tree Maximum Path Sum (tree)
- LeetCode Binary Tree Maximum Path Sum
- 大数据点滴
- 密钥仓库(证书导入keystore)
- 实现ListView图文混排 —— 获取Json数据
- java 文件处理总结
- 欢迎使用CSDN-markdown编辑器
- LeetCode 124:Binary Tree Maximum Path Sum
- java Pattern和Matcher详解
- RS422接线方法
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 前端开发入门:前端开发环境构建工具Grunt
- 如何查找一个新领域的景点文献
- UIWindow对象的获得方式
- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver