2 条题解
-
0Michstabe LV 1 @ 2024-10-7 11:36:38
#include <bits/stdc++.h> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; int sum(TreeNode* root) { if (root==NULL) return 0; if (root->left==NULL&&root->right==NULL) return root->val; return sum(root->left) + sum(root->right); }TreeNode* build(vector<int>& t,int index) { if (index>=t.size()) return NULL; TreeNode* node = new TreeNode(t[index]); node->left=build(t,2 * index + 1); node->right=build(t,2 * index + 2); return node; } int main() { int n; while (cin >> n && n!= 0) { vector<int> t(n); for (int i = 0; i < n; ++i) cin>>t[i]; TreeNode* root=build(t,0); cout<<sum(root)<<endl; } }
-
02024-10-4 23:08:47@
- 1
信息
- ID
- 3186
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 7
- 标签
- (无)
- 递交数
- 28
- 已通过
- 8
- 上传者