Untitled
*******************************************************************************/ #include <iostream> #include <vector> using namespace std; int main() { vector<int> a(30, 0); // Initialize array with 30 elements as 0 int min = 10000, temp = 0, n, noofc, z; // Input number of nodes cout << "Please enter how many numbers: "; cin >> n; // Input root value cout << "Enter value of root: "; cin >> a[0]; // Build the tree for (int i = 1; i <= n / 2; i++) { cout << "Please enter no. of child of parent with value " << a[i - 1] << ": "; cin >> noofc; for (int j = 1; j <= noofc; j++) { z = i * 2 + j - 2; // Child index if (z >= 30) { cout << "Exceeded tree size. Skipping.\n"; break; } cout << "Please enter value of child: "; cin >> a[z]; } } // Calculate minimum path sum for (int i = n - 1; i >= n / 2; i--) { temp = 0; for (int j = i + 1; j >= 1; j = j / 2) temp += a[j - 1]; if (temp < min) min = temp; cout << "Temp min is: " << temp << "\n"; } cout << "Min is: " << min << endl; return 0; }
Leave a Comment