Binary search tree
조회수 121회
이진 검색 트리의 높이를 결정하기 위해 이 접근 방식을 재작업하는 데 도움을 줄 수 있는 사람이 있는지 궁금합니다. 지금까지 내 코드는 다음과 같습니다.
public int findHeight(){
if(this.isEmpty()){
return 0;
}
else{
TreeNode<T> node = root;
return findHeight(node);
}
}
private int findHeight(TreeNode<T> aNode){
int heightLeft = 0;
int heightRight = 0;
if(aNode.left!=null)
heightLeft = findHeight(aNode.left);
if(aNode.right!=null)
heightRight = findHeight(aNode.right);
if(heightLeft > heightRight){
return heightLeft+1;
}
else{
return heightRight+1;
}
}
그러나 내가 얻는 대답은 내 실제 키보다 하나 더 높습니다. 하지만 이 소스를 읽은 후 반환 문에서 +1을 제거하면 실제 높이가 1만큼 줄어드는 것을 알고 있습니다. 이 BST를 사용하여 재귀를 알아내려고 계속 노력하고 있습니다. 도움을 주시면 감사하겠습니다.
댓글 입력