From 9cadfd058d7926f891838f87469a236fc110d354 Mon Sep 17 00:00:00 2001 From: linlihong <747682928@qq.com> Date: Tue, 3 Sep 2024 10:01:27 +0800 Subject: [PATCH] =?UTF-8?q?2024/9/3=20Hot100=20binaryTree=20=E6=9C=80?= =?UTF-8?q?=E5=90=8E=E4=B8=80=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/cool/hot100/binary_tree/Num124.java | 31 +++++++++++++++++++ .../com/cool/hot100/binary_tree/Num543.java | 11 ++++--- 2 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/cool/hot100/binary_tree/Num124.java diff --git a/src/main/java/com/cool/hot100/binary_tree/Num124.java b/src/main/java/com/cool/hot100/binary_tree/Num124.java new file mode 100644 index 0000000..757707b --- /dev/null +++ b/src/main/java/com/cool/hot100/binary_tree/Num124.java @@ -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); + } +} diff --git a/src/main/java/com/cool/hot100/binary_tree/Num543.java b/src/main/java/com/cool/hot100/binary_tree/Num543.java index 1f928a9..ca03959 100644 --- a/src/main/java/com/cool/hot100/binary_tree/Num543.java +++ b/src/main/java/com/cool/hot100/binary_tree/Num543.java @@ -11,20 +11,21 @@ package com.cool.hot100.binary_tree; */ public class Num543 { - int max=0; + int max = 0; + public int diameterOfBinaryTree(TreeNode root) { if (root == null) return 0; int left = getTreeDeep(root.left); int right = getTreeDeep(root.right); - return Math.max(max,left+right); + return Math.max(max, left + right); } private int getTreeDeep(TreeNode root) { if (root == null) return 0; int left = getTreeDeep(root.left); - int right=getTreeDeep(root.right); - max = Math.max(max,left+right ); - return Math.max(left,right)+1; + int right = getTreeDeep(root.right); + max = Math.max(max, left + right); + return Math.max(left, right) + 1; } }