LeetCode(124) Binary Tree Maximum Path Sum
来源:互联网 发布:瓷砖上铺木地板 知乎 编辑:程序博客网 时间:2024/05/17 07:26
题目如下:
Given 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.
分析如下:
注意从NODE A开始向叶节点方向增长的的maximum path一共有一下这4种情况
1 NODE, 如下面这种情况,最大值就是node(value = 1)自己。
1
/ \
-2 -3
2 NODE + left child
1
/ \
2 -3
3 NODE + right child
1
/ \
-2 3
4 NODE + right child + right child
1
/ \
2 3
其中除了4是折线(下面代码中的twofold_path_max)外,1~3都是直线(下面代码中额left_onefold_max 和right_onefold_max)。
对于每个节点算出的这四个值,都和保存当前最大值的变量maximum_result进行比较,不断地刷新maximum_result的值。
我的代码:
/** * 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 my_maxPathSum(TreeNode *root, int onefold_path_max, int twofold_path_max, int& max_result) { if (root == NULL) return 0; int left_onefold_max = my_maxPathSum(root->left, onefold_path_max, twofold_path_max, max_result); int right_onefold_max = my_maxPathSum(root->right, onefold_path_max, twofold_path_max, max_result); int return_one_fold_max = 0; twofold_path_max = left_onefold_max + right_onefold_max + root->val; // node + left + right,作为最大值的候选 left_onefold_max = (left_onefold_max)>0?(left_onefold_max + root->val):(root->val); //从node和node + left中选个最大值 right_onefold_max = (right_onefold_max)>0?(right_onefold_max + root->val):(root->val);//从node和node + right中选个最大值 return_one_fold_max = (right_onefold_max > left_onefold_max)? right_onefold_max:left_onefold_max; max_result = (max_result > right_onefold_max)? max_result:right_onefold_max; //Note: max_result是轮流地和candidate比较 max_result = (max_result > left_onefold_max)? max_result:left_onefold_max; max_result = (max_result > twofold_path_max)? max_result:twofold_path_max; return return_one_fold_max; } int maxPathSum(TreeNode *root) { int max_result = INT_MIN; int onefold_path_max = 0; int twofold_path_max = 0; my_maxPathSum(root, onefold_path_max, twofold_path_max, max_result); return max_result; }};
0 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
- 为什么全世界只有13台根域名解析服务器
- Android从入门到精通第11章:Content Provider实现数据共享
- Unity手机优化注意点
- 用java对密码进行MD5加密
- php通过两个地点经纬度求距离
- LeetCode(124) Binary Tree Maximum Path Sum
- C语言使用socket获取网页内容
- Android三步显示gif动态图片
- 程序设计语言(脑图)
- 关于小公司的空降兵和空降兵的出路
- 关于APK第一次安装后按Home键出现的问题
- 鸟哥笔记——关于变量的基本配置
- 获取手机所有应用程序的信息
- 编写函数计算n的阶乘(n<10),n为函数参数。在main函数中调用该函数计算1!+2!+……+n!