Skip to content

Commit 4209297

Browse files
committed
update
1 parent ae1a8fc commit 4209297

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

leetcode_solved/[editing]leetcode_0124_Binary_Tree_Maximum_Path_Sum.cpp

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* AC: T:O(logn) S:O(logn) (递归栈调用消耗)
3+
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Binary Tree Maximum Path Sum.
4+
* Memory Usage: 40.5 MB, less than 92.68% of Java online submissions for Binary Tree Maximum Path Sum.
5+
*
6+
* 思路:树的原始递归,重点在于理解题意,一个子树要么直接贡献一条最大和路径,要么共享 root+max(左子树递归,右子树递归).用一个外部变量记录中间可能产生的最大值.
7+
*/
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode() {}
15+
* TreeNode(int val) { this.val = val; }
16+
* TreeNode(int val, TreeNode left, TreeNode right) {
17+
* this.val = val;
18+
* this.left = left;
19+
* this.right = right;
20+
* }
21+
* }
22+
*/
23+
class Solution {
24+
public int maxPathSum(TreeNode root) {
25+
int leftMax = maxPathSum1(root.left);
26+
int rightMax = maxPathSum1(rootright);
27+
28+
return
29+
}
30+
public int maxPathSum1(TreeNode root) {
31+
if (root == null) {
32+
return 0;
33+
}
34+
if (root.left == null && root.right == null) {
35+
return root.val;
36+
}
37+
int left = maxPathSum(root.left);
38+
int right = maxPathSum(root.right);
39+
return root.val + (left > right ? left : right);
40+
}
41+
}

0 commit comments

Comments
 (0)