두 개의 이진 트리를 교차시키고 동일한 노드로 새 이진 트리를 만들려고하지만 다음과 같이하면 stackOverflow 오류가 발생합니다. 누구든지 나를 도울 수 있습니까?2 이진 트리의 교차점이 스택 오버플로 오류를 던졌습니다
private OrderedSet<E> resultIntersect = new OrderedSet<E>();
public OrderedSet<E> intersection(OrderedSet<E> other) {
OrderedSet<E> result = new OrderedSet<E>();
if (other.root == null || root == null)
return result;
else if (height() == 0 && other.height() == 0
&& other.root.data.equals(root.data)) {
result.insert(root.data);
return result;
} else {
intersection(other, root, other.root);
result = resultIntersect;
}
return result;
}
private void intersection(OrderedSet<E> other, TreeNode root1,
TreeNode root2) {
if (root1 == root2) {
resultIntersect.insert(root1.data);
}
if (root1 == null || root2 == null) {
return;
}
intersection(other, root1.left, root2.left);
intersection(other, root1.right, root2.right);
}
편집 나는이 느낌
내가 그것을해야하는지에 가까운,하지만 난 여전히 오류가 발생합니다.
private OrderedSet<E> resultIntersect = new OrderedSet<E>();
public OrderedSet<E> intersection(OrderedSet<E> other) {
OrderedSet<E> result = new OrderedSet<E>();
result = resultIntersect;
return result;
}
private void intersection(OrderedSet<E> other, TreeNode t) {
if (other.contains(t.data)) {
resultIntersect.insert(t.data);
}
if(t.left != null)
intersection(other, t.left);
if(t.right != null)
intersection(other, t.right);
}
나무가 얼마나 큽니까? 아마도 최대 재귀 깊이에 도달 할 수 있습니까? – nbrooks
제가 테스트하는 나무는 매우 작습니다. 4-5 개의 노드입니다. – Tinkerbell
이것은 내 테스트입니다. – Tinkerbell