【记录】int-->CString

int j = 10;
CString str;
str.Format(_T("%d"), j);
MessageBox(str);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是求解二叉搜索树后序遍历的根节点到所有叶子节点的路径及路径上所有节点的和的C++代码: ```c++ #include <iostream> #include <cstring> #include <algorithm> #include <vector> using namespace std; const int N = 1010; int postorder[N]; vector<int> path; int n; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; TreeNode *buildTree(int left, int right) { if (left > right) return nullptr; TreeNode *root = new TreeNode(postorder[right]); int k = left; while (k < right && postorder[k] < postorder[right]) k++; root->left = buildTree(left, k - 1); root->right = buildTree(k, right - 1); return root; } void dfs(TreeNode *root, int sum) { if (!root) return; sum += root->val; path.push_back(root->val); if (!root->left && !root->right) { cout << sum << ':'; for (auto x : path) cout << ' ' << x; cout << endl; } dfs(root->left, sum); dfs(root->right, sum); path.pop_back(); } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> postorder[i]; TreeNode *root = buildTree(0, n - 1); dfs(root, 0); return 0; } ``` 思路:根据二叉搜索树的性质,后序遍历序列的最后一个数为根节点,将其分为左子树和右子树,递归构建二叉搜索树。使用深度优先搜索遍历每个节点,记录下路径和路径上的节点值,当遍历到叶子节点时,输出路径和路径上的节点值的和。由于是深度优先搜索,需要记录下每个节点在路径上的位置,因此使用一个vector容器记录当前路径中节点的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值