29 lines
606 B
Java
29 lines
606 B
Java
package com.cool.hot100.binary_tree;
|
|
|
|
/**
|
|
* Created with IntelliJ IDEA.
|
|
*
|
|
* @Author: Cool
|
|
* @Date: 2024/08/28/23:52
|
|
* DayNumber 5
|
|
* Hard 1
|
|
* Level 3
|
|
*/
|
|
public class Num543 {
|
|
|
|
public int diameterOfBinaryTree(TreeNode root) {
|
|
if (root == null) return 0;
|
|
int left = getTreeDeep(root.left);
|
|
int right = getTreeDeep(root.right);
|
|
return 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;
|
|
}
|
|
|
|
}
|