2024/9/3 Hot100 binaryTree 最后一题
This commit is contained in:
parent
8fc3fadc94
commit
9cadfd058d
|
@ -0,0 +1,31 @@
|
||||||
|
package com.cool.hot100.binary_tree;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @Author: Cool
|
||||||
|
* @Date: 2024/09/03/09:37
|
||||||
|
* DayNumber 1
|
||||||
|
* Hard 3
|
||||||
|
* Level ?
|
||||||
|
*/
|
||||||
|
public class Num124 {
|
||||||
|
|
||||||
|
int res = Integer.MIN_VALUE;
|
||||||
|
|
||||||
|
public int maxPathSum(TreeNode root) {
|
||||||
|
dfs(root);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int dfs(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int left = Math.max(0, dfs(root.left));
|
||||||
|
int right = Math.max(0, dfs(root.right));
|
||||||
|
|
||||||
|
res = Math.max(res, root.val + left + right);
|
||||||
|
return root.val + Math.max(left,right);
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,6 +12,7 @@ package com.cool.hot100.binary_tree;
|
||||||
public class Num543 {
|
public class Num543 {
|
||||||
|
|
||||||
int max = 0;
|
int max = 0;
|
||||||
|
|
||||||
public int diameterOfBinaryTree(TreeNode root) {
|
public int diameterOfBinaryTree(TreeNode root) {
|
||||||
if (root == null) return 0;
|
if (root == null) return 0;
|
||||||
int left = getTreeDeep(root.left);
|
int left = getTreeDeep(root.left);
|
||||||
|
|
Loading…
Reference in New Issue