-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBSTHeight.java
More file actions
33 lines (28 loc) · 822 Bytes
/
Copy pathBSTHeight.java
File metadata and controls
33 lines (28 loc) · 822 Bytes
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
package Unit_5_Labs_Tree_Heaps_PQs;
public class BSTHeight<TKey extends Comparable<TKey>, TValue> {
class Node {
TKey key;
TValue value;
Node left;
Node right;
}
Node root;
public int height() {
return root == null ? 0 : height(root);
}
private int height(Node current) {
if (current == null) return -1;
int leftHeight = height(current.left);
int rightHeight = height(current.right);
return Math.max(leftHeight, rightHeight) + 1;
}
// This is used by our test code. Do not change.
Node newNode(TKey key, TValue value, Node left, Node right) {
Node node = new Node();
node.key = key;
node.value = value;
node.left = left;
node.right = right;
return node;
}
}