Jestem nowy w rekursji i próbuję zrozumieć ten fragment kodu. Uczę się na egzamin, a to jest "recenzent", którego znalazłem w Standford CIS Education Library (From Binary Trees, Nick Parlante).Jak działa rekurencja wewnątrz pętli
Rozumiem koncepcję, ale kiedy recytujemy W PĘTLI, to wszystko wieje! Proszę pomóż mi. Dziękuję Ci.
countTrees() rozwiązanie (C/C++)
/*
For the key values 1...numKeys, how many structurally unique
binary search trees are possible that store those keys.
Strategy: consider that each value could be the root.
Recursively find the size of the left and right subtrees.
*/
int countTrees(int numKeys) {
if (numKeys <=1) {
return(1);
}
// there will be one value at the root, with whatever remains
// on the left and right each forming their own subtrees.
// Iterate through all the values that could be the root...
int sum = 0;
int left, right, root;
for (root=1; root<=numKeys; root++) {
left = countTrees(root - 1);
right = countTrees(numKeys - root);
// number of possible trees with this root == left*right
sum += left*right;
}
return(sum);
}
Powiedziałbym, że działa dokładnie w ten sam sposób, ale myślę, że nie jest to odpowiedź, jakiej oczekujesz. Nie pozwólcie, aby wasz "głupiec" - zakres, tutaj się liczył. – rsenna
Jak masz na myśli "to wszystko wieje"? Czy masz awarię? A może twój mózg się sprzeciwia? – Arkadiy