二叉树遍历应用(dfs)124. Binary Tree Maximum Path Sum
来源:互联网 发布:java单例模式双重锁 编辑:程序博客网 时间:2024/06/05 10:44
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */// LeetCode, Binary Tree Maximum Path Sum// 时间复杂度O(n),空间复杂度O(logn)class Solution {public: int maxSum = -99999999; int maxPathSum(TreeNode *root) { if (!root) return 0; dfs(root); return maxSum; } int dfs(TreeNode *root) { if (!root) return 0; int sum = root->val; int l = dfs(root->left); int r = dfs(root->right); if (l>0) sum+=l; if(r>0) sum+=r; maxSum = max(sum, maxSum); return max(l,r)>0? max(l,r)+root->val: root->val; }};
不是最优
0 0
- 二叉树遍历应用(dfs)124. Binary Tree Maximum Path Sum
- 【DFS&二叉树】Binary Tree Maximum Path Sum
- [LeetCode 124] Binary Tree Maximum Path Sum (DFS/二叉树)
- 二叉树-Binary tree maximum path sum
- DFS Binary Tree Maximum Path Sum
- [leetcode][dfs] Binary Tree Maximum Path Sum
- 124:Binary Tree Maximum Path Sum【树】【DFS】
- Binary Tree Maximum Path Sum 二叉树最大路径和
- Tree-----124. Binary Tree Maximum Path Sum
- LeetCode 124. Binary Tree Maximum Path Sum(二叉树最大路径和)
- 【二叉树】最大路径和【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] 124. Binary Tree Maximum Path Sum
- Leetcode 124. Binary Tree Maximum Path Sum
- --Listener (一)Listener简介
- 快排找第k大(小)元素
- Restful CRUD
- 后记
- c++primer第十五章面向对象设计小结-15
- 二叉树遍历应用(dfs)124. Binary Tree Maximum Path Sum
- Windows下Python多版本共存
- Android进阶之自定义注解
- html5 css布局
- 继续内存结构问题
- MySQL存储引擎中的MyISAM和InnoDB区别
- push 和 pop 函数的使用
- Material Design控件使用(一)
- POJ Problem 1017 Packets