[124]. Binary Tree Maximum Path Sum, [543]. Diameter of Binary Tree
来源:互联网 发布:淘宝授权书怎么写 编辑:程序博客网 时间:2024/05/17 08:19
[124]. Binary Tree Maximum Path Sum
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 must contain at least one node and does not need to go through the root.
For example:Given the below binary tree, 1 / \ 2 3Return 6.
这题基本的做法还是可以分成,最大路径出现在左子树,右子树,以及通过根节点三种情况讨论,复杂度为O(nlogn).复杂度理解参照快排。
代码:
class Solution {public: int maxPathSum(TreeNode* root) { if (root == NULL) return INT_MIN; if (root->left == NULL && root->right == NULL) return root->val; int maxleft = maxPathSum(root->left); int maxright = maxPathSum(root->right); int maxltor = root->val; int maxleftconn = maxconn(root->left, 0); int maxrightconn = maxconn(root->right, 0); return max(max(maxleft, maxright), maxltor + maxleftconn + maxrightconn); } int maxconn(TreeNode* root, int prevSum) { if (root == NULL) return prevSum; if (root->left == NULL && root->right == NULL) return max(prevSum, prevSum + root->val); int maxleftconn = maxconn(root->left, root->val); int maxrightconn = maxconn(root->right, root->val); return max(prevSum, prevSum + max(maxleftconn, maxrightconn)); }};
方法二:
这题其实可以将左右子树中的判断和通过根节点的判断融为一体,减少不必要的递归。
可以记住这种很常用的处理手段。因为实际上所有的判断归根结底都是发生在寻找左边最大路径,右边最大路径,不论是左右相加还是左右取较大,都是基于同样的操作。除了递归,我们可以维持一个maxsum的数来记录所有可能的路径中的最大和。
代码:
class Solution {public: int maxPathSum(TreeNode* root) { int maxsum = INT_MIN; DFS(root, maxsum); return maxsum; } int DFS(TreeNode* root, int& maxsum) { if (root == NULL) return 0; int left = max(0, DFS(root->left, maxsum)); int right = max(0, DFS(root->right, maxsum)); maxsum = max(maxsum, root->val + left + right); return max(root->val, root->val + max(left, right)); }};
复杂度为O(n).
同样的操作可以用在另一题上:
543. Diameter of Binary Tree
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
Example:Given a binary tree 1 / \ 2 3 / \ 4 5 Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
Note: The length of path between two nodes is represented by the number of edges between them.
class Solution {public: int diameterOfBinaryTree(TreeNode* root) { int diameter = 0; DFS(root, diameter); return diameter; } int DFS(TreeNode* root, int& diameter) { if (root == NULL) return 0; int left = DFS(root->left, diameter); int right = DFS(root->right, diameter); diameter = max(diameter, left + right); return max(left, right) + 1; }};
- [124]. Binary Tree Maximum Path Sum, [543]. Diameter of Binary Tree
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- 随机森林
- 比top更好用的进程监控工具htop
- [学习笔记][Java编程思想]第5章:初始化与清理
- win10 如何禁止特定路径下的exe启动程序
- MySQL函数大全及用法示例
- [124]. Binary Tree Maximum Path Sum, [543]. Diameter of Binary Tree
- Effective Java中文第四章第22节(个人渣翻)
- leetcode 202. Happy Number
- 一个字符占几个字节
- session技术—将商品加入购物车、验证码校验
- 关于memcpy和memmove两函数的区别
- 软件测试培训和自学的区别
- Map分支—TreeMap
- HDU:2030 汉字统计