-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTreeDiameter.java
37 lines (33 loc) · 1.1 KB
/
BinaryTreeDiameter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.*;
class Program {
// This is an input class. Do not edit.
static class BinaryTree {
public int value;
public BinaryTree left = null;
public BinaryTree right = null;
public BinaryTree(int value) {
this.value = value;
}
}
// O(n) time | O(h) space
public int binaryTreeDiameter(BinaryTree tree) {
// Write your code here.
return helper(tree)[0];
}
private static int[] helper(BinaryTree root) {
if (root == null) {
return new int[] { 0, 0 };
}
int[] leftTreeDiameterAndHeight = helper(root.left);
int[] rightTreeDiameterAndHeight = helper(root.right);
int leftTreeDiameter = leftTreeDiameterAndHeight[0];
int leftTreeHeight = leftTreeDiameterAndHeight[1];
int rightTreeDiameter = rightTreeDiameterAndHeight[0];
int rightTreeHeight = rightTreeDiameterAndHeight[1];
int maxDiameterSoFar = Math.max(leftTreeDiameter, rightTreeDiameter);
int longestPath = leftTreeHeight + rightTreeHeight;
int maxDiameter = Math.max(maxDiameterSoFar, longestPath);
int height = 1 + Math.max(leftTreeHeight, rightTreeHeight);
return new int[] { maxDiameter, height };
}
}