Refactor method for calculating height
This commit is contained in:
parent
0a4895c538
commit
63da19757e
1 changed files with 11 additions and 6 deletions
|
@ -74,14 +74,19 @@ public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
|
|||
}
|
||||
|
||||
protected int calcHeight(TreeNode<T> node) {
|
||||
int result = 0;
|
||||
if (node == null) {
|
||||
return 0;
|
||||
} else {
|
||||
int result = 1;
|
||||
|
||||
if (node.left != null || node.right != null) {
|
||||
result = Stream.of(node.left, node.right).filter(
|
||||
(innerNode) -> innerNode != null).mapToInt((innerNode) -> calcHeight(innerNode)).max().getAsInt();
|
||||
result += Stream.of(node.left, node.right).filter(
|
||||
(innerNode) -> innerNode != null).mapToInt((innerNode) -> calcHeight(innerNode)).max()
|
||||
.getAsInt();
|
||||
}
|
||||
|
||||
return result + 1;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public int height() {
|
||||
|
|
Loading…
Reference in a new issue