2024/8/28 LeetCode Hot100 binaryTree Num543解决

This commit is contained in:
Cool 2024-08-29 20:10:44 +08:00
parent 4dbf7ab57e
commit abacb92f43
1 changed files with 6 additions and 4 deletions

View File

@ -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;
}
}