트리의 자식 수를 셀 수있는 LinkedBinaryTree 클래스의 메서드로 작업하고 있습니다. 내 코드는 아래에 있지만 드라이버에서 실행할 때 무한 루프가 발생합니다.Java - LinkedBinaryTree의 자식 수 찾기
public int children(BinaryTreeNode<T> node) {
int children = 0;
if(node.getLeft() != null){
children = 1 + children(node);
}
else if(node.getRight() != null){
children = children + 1 + children(node);
}
return children;
}
특히,이 StackOverflow의 오류가 발생이 줄은, 그리고 나는 그것을 넘어 이동할 수 없습니다 :
children = 1 + children(node);
누구나 어떤 생각이 내 코드를 수정할 수 있도록하는 방법? 내 논리로 간과 할 것이 없나요? 도움을 감사하십시오.
, 그것은'node.getLeft()'와'node.getRight()'를 각각해야합니다. –