We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e52316a commit dce197eCopy full SHA for dce197e
BinaryTreeDiameter.java
@@ -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