-
Notifications
You must be signed in to change notification settings - Fork 22
/
DiameterOfBinaryTree.java
86 lines (63 loc) · 2.65 KB
/
DiameterOfBinaryTree.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.company.amazon;
import java.util.Stack;
import static com.company.amazon.BinaryTree.*;
public class DiameterOfBinaryTree {
private static class Path {
Stack<Integer> stack = new Stack<>();
public Stack<Integer> getStack() {
return this.stack;
}
public void setStack(Stack<Integer> stack) {
this.stack = stack;
}
}
private static class Height {
int height;
}
public static void main(String[] args) {
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
System.out.println("Get Diameter :: " + getDiameter(root));
System.out.println("Get Diameter Optimized ::" + getDiameterOptimized(root, new Height()));
System.out.print("getDiameterOptimizedWithStaticVariable :: ");
getDiameterOptimizedWithStaticVariable(root);
System.out.println(MAX_DIAMETER);
}
public static int getDiameter(Node root) {
if (root == null) {
return 0;
}
int leftHeight = getHeight(root.left);
int rightHeight = getHeight(root.right);
int heightIncludingRoot = leftHeight + rightHeight + 1;
return Math.max(heightIncludingRoot, Math.max(getDiameter(root.left), getDiameter(root.right)));
}
public static int getHeight(Node root) {
if (root == null)
return 0;
return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
}
public static int getDiameterOptimized(Node root, Height height) {
Height leftHeight = new Height();
Height rightHeight = new Height();
if (root == null)
return 0;
int leftDiameter = getDiameterOptimized(root.left, leftHeight);
int rightDiameter = getDiameterOptimized(root.right, rightHeight);
int heightOfCurrentNode = Math.max(leftHeight.height, rightHeight.height) + 1;
height.height = heightOfCurrentNode;
return Math.max((leftHeight.height + rightHeight.height + 1), Math.max(leftDiameter, rightDiameter));
}
private static int MAX_DIAMETER = 0;
public static int getDiameterOptimizedWithStaticVariable(Node root) {
if (root == null)
return 0;
int leftDiameter = getDiameterOptimizedWithStaticVariable(root.left);
int rightDiameter = getDiameterOptimizedWithStaticVariable(root.right);
MAX_DIAMETER = Math.max((leftDiameter + rightDiameter + 1), MAX_DIAMETER); // Plus 1 because we are including the root
return Math.max(leftDiameter, rightDiameter) + 1;
}
}