0
현재 이진 검색 트리 전체에서 최대 불균형을 반환하는 재귀 적 메서드를 코딩하고 있습니다. 재귀 프로그래밍에 새로운 것이므로 내 머리를 감싸는 것은 매우 어렵습니다. 내가 만든 나무는 불균형이 1이지만 내 방법은 0 만 반환합니다. 여기 내 논리에는 결함이있는 것으로 확신합니다.이진 검색 트리의 최대 불균형 계산
메소드의 모든 단계에서 실행중인 "(root == null) {return 0;}"입니다. 내가 그것을 제거하고 그것을 더 정의하고 그것을 동일하게 계속 노력했다.
이 내 현재의 방법입니다 :
이public int getMaxImbalance(){
return Math.abs(getMaxImbalance(root));
}
public int getMaxImbalance (TreeNode<E> root){
if (root == null){
return 0;
}else if(root.left != null && root.right == null){
return 1 + getMaxImbalance(root.left) + getMaxImbalance(root.right);
//adds 1 left is true and right is false
}else if(root.left == null && root.right != null){
return -1 + getMaxImbalance(root.left) + getMaxImbalance(root.right);
//adds -1 left is false and right is true
}
return getMaxImbalance(root.left) + getMaxImbalance(root.right);
//calls itself if both fields are null;
}