From abacb92f437e380524ed5a1091d8e7fdeca8882b Mon Sep 17 00:00:00 2001 From: Cool <747682928@qq.com> Date: Thu, 29 Aug 2024 20:10:44 +0800 Subject: [PATCH] =?UTF-8?q?2024/8/28=20LeetCode=20Hot100=20binaryTree=20Nu?= =?UTF-8?q?m543=E8=A7=A3=E5=86=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/cool/hot100/binary_tree/Num543.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) 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 f0abcc5..1f928a9 100644 --- a/src/main/java/com/cool/hot100/binary_tree/Num543.java +++ b/src/main/java/com/cool/hot100/binary_tree/Num543.java @@ -11,18 +11,20 @@ package com.cool.hot100.binary_tree; */ public class Num543 { + int max=0; public int diameterOfBinaryTree(TreeNode root) { if (root == null) return 0; int left = getTreeDeep(root.left); int right = getTreeDeep(root.right); - return left + right; + return Math.max(max,left+right); } private int getTreeDeep(TreeNode root) { if (root == null) return 0; - int res = getTreeDeep(root.left); - res = Math.max(res, getTreeDeep(root.right)); - return res + 1; + int left = getTreeDeep(root.left); + int right=getTreeDeep(root.right); + max = Math.max(max,left+right ); + return Math.max(left,right)+1; } }