2024/8/28 LeetCode Hot100 binaryTree Num543解决
This commit is contained in:
parent
4dbf7ab57e
commit
abacb92f43
|
@ -11,18 +11,20 @@ package com.cool.hot100.binary_tree;
|
||||||
*/
|
*/
|
||||||
public class Num543 {
|
public class Num543 {
|
||||||
|
|
||||||
|
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);
|
||||||
int right = getTreeDeep(root.right);
|
int right = getTreeDeep(root.right);
|
||||||
return left + right;
|
return Math.max(max,left+right);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getTreeDeep(TreeNode root) {
|
private int getTreeDeep(TreeNode root) {
|
||||||
if (root == null) return 0;
|
if (root == null) return 0;
|
||||||
int res = getTreeDeep(root.left);
|
int left = getTreeDeep(root.left);
|
||||||
res = Math.max(res, getTreeDeep(root.right));
|
int right=getTreeDeep(root.right);
|
||||||
return res + 1;
|
max = Math.max(max,left+right );
|
||||||
|
return Math.max(left,right)+1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue