leetcode---binary-tree-maximum-path-sum---树
来源:互联网 发布:plsql输入ip连接数据 编辑:程序博客网 时间:2024/05/29 17:05
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
Return6.
/** * 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 ans; int dfs(TreeNode *root) { if(!root) return 0; int lmax = dfs(root->left); int rmax = dfs(root->right); int v = max(root->val, max(root->val+lmax, root->val+rmax)); int v1 = max(v, root->val+lmax+rmax); if(v1 > ans) ans = v1; return v; } int maxPathSum(TreeNode *root) { ans = INT_MIN; dfs(root); return ans; }};
阅读全文
0 0
- leetcode---binary-tree-maximum-path-sum---树
- *****(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
- 数据结构——顺序表最大子段和分治递归法
- 一个web前端初学者对html的简单理解
- 拉格朗日插值法(求自然数幂和)
- Python运算符
- 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
- leetcode---binary-tree-maximum-path-sum---树
- bzoj 4765: 普通计算姬(分块+树状数组)
- 论文投稿注意事项
- 求数组最大值的下标
- Servlet生命周期
- linux 下的软件的安装与卸载
- BZOJ1051(洛谷P2341) [HAOI2006]受欢迎的牛
- 大整数的因子
- 软考经验