Skip to content

Commit dce197e

Browse files
authored
Create BinaryTreeDiameter.java
1 parent e52316a commit dce197e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

BinaryTreeDiameter.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
3+
int sol;
4+
5+
public int diameterOfBinaryTree(TreeNode root) {
6+
7+
sol = 0;
8+
9+
height(root);
10+
11+
return sol;
12+
13+
}
14+
15+
private int height(TreeNode root) {
16+
if (root == null) return 0;
17+
18+
int leftHeight = height(root.left);
19+
int rightHeight = height(root.right);
20+
21+
sol = Math.max(
22+
sol,
23+
leftHeight + rightHeight
24+
);
25+
26+
return Math.max(leftHeight, rightHeight) + 1;
27+
}
28+
}

0 commit comments

Comments
 (0)