मैं रिकर्सन और इस कोड स्निपेट को समझने की कोशिश करने के लिए नया हूं। मैं एक परीक्षा के लिए पढ़ रहा हूं, और यह एक "समीक्षक" है जो मुझे स्टैंडफोर्ड 'सीआईएस एजुकेशन लाइब्रेरी (निक पार्लैंट द्वारा बाइनरी पेड़ से) से मिला है।कैसे लूप के लिए रिकर्सन वर्क्स के अंदर काम करता है
मैं अवधारणा को समझता हूं, लेकिन जब हम लूप के अंदर रिकर्स कर रहे हैं, तो यह सब उड़ाता है! क्रिप्या मेरि सहायता करे। धन्यवाद।
countTrees() समाधान (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);
}
मैं कहूंगा कि यह ठीक उसी तरह काम करता है, लेकिन मुझे लगता है कि यह आपके जैसा जवाब नहीं है। 'मूर्ख' को मूर्ख मत होने दें - दायरा यहां मायने रखता है। – rsenna
आपका मतलब क्या है "यह सब उड़ाता है"? क्या आपको दुर्घटना हो रही है? या आपका मस्तिष्क झुका रहा है? – Arkadiy