structTreeNode { int val; TreeNode* left; TreeNode* right; };
TreeNode* create(int postL, int postR, int inL, int inR){ if (postL > postR) returnNULL; TreeNode* root = new TreeNode; root -> val = postOrder[postR]; int k; for (k = inL; k <= inR; k++) if (inOrder[k] == postOrder[postR]) break; int numberLeft = k - inL; root -> left = create(postL, postL + numberLeft - 1, inL, k - 1); root -> right = create(postL + numberLeft, postR - 1, k + 1, inR); return root; }
voidlevelTraversal(TreeNode* root, int n){ if (!root) return; queue<TreeNode*> q; q.push(root); int num = 0; while(!q.empty()) { TreeNode* cur = q.front(); q.pop(); cout << cur -> val; num++; if (num < n) cout << " "; if (cur -> left) q.push(cur -> left); if (cur -> right) q.push(cur -> right); } return; }
intmain(){ int n; cin >> n; postOrder.resize(n); inOrder.resize(n); for (int i = 0; i < n; i++) cin >> postOrder[i]; for (int i = 0; i < n; i++) cin >> inOrder[i]; TreeNode* root = create(0, n-1, 0, n-1); levelTraversal(root, n); return0; }
#include<bits/stdc++.h> usingnamespace std; int n, num = 0; vector<int> preOrder, inOrder;
structTreeNode { int val; TreeNode* left; TreeNode* right; };
TreeNode* create(int preL, int preR, int inL, int inR){ if (preL > preR) returnNULL; TreeNode* root = new TreeNode; root -> val = preOrder[preL]; int k; for (k = inL; k <= inR; k++) if (inOrder[k] == preOrder[preL]) break; int numLeft = k - inL; root -> left = create(preL + 1, preL + numLeft, inL, k - 1); root -> right = create(preL + numLeft + 1, preR, k + 1, inR); return root; }
structTreeNode { int val; TreeNode* left; TreeNode* right; };
voidinsert(TreeNode* &root){ if (mp[root->val].first < 0 && mp[root->val].second < 0) return; if (mp[root->val].first >= 0) { TreeNode* l = new TreeNode; l -> val = mp[root->val].first; root -> left = l; insert(root -> left); } else root -> left = NULL; if (mp[root->val].second >= 0) { TreeNode* r = new TreeNode; r -> val = mp[root->val].second; root -> right = r; insert(root -> right); } else root -> right = NULL; return; }
voidlevelTraversal(TreeNode* root){ if (!root) return; queue<TreeNode*> q; q.push(root); int num = 0; while(!q.empty()) { TreeNode* cur = q.front(); q.pop(); cout << cur -> val; num++; if (num < n) cout << " "; if (cur -> left) q.push(cur -> left); if (cur -> right) q.push(cur -> right); } cout << endl; return; }
int num = 0; voidinOrderTraversal(TreeNode* root){ if (!root) return; inOrderTraversal(root -> left); cout << root -> val; num++; if (num < n) cout << " "; inOrderTraversal(root -> right); return; }
intmain(){ cin >> n; isRoot.resize(n, true); for (int i = 0; i < n; i++) { char s1, s2; cin >> s1 >> s2; int a, b; if (s1 == '-') a = -1; else a = s1 - '0'; if (s2 == '-') b = -1; else b = s2 - '0'; if (a != -1) isRoot[a] = false; if (b != -1) isRoot[b] = false; mp[i] = make_pair(b, a); } int i; for (i = 0; i < n; i++) if (isRoot[i]) break; TreeNode* root = new TreeNode; root -> val = i; insert(root); levelTraversal(root); inOrderTraversal(root); return0; }
#include<bits/stdc++.h> usingnamespace std; int n, m, s; vector<int> temp; vector<vector<int>> ans;
structTreeNode { int weight; vector<int> child; } treeNode[110];
voiddfs(int index, int sum){ if (sum == s && !treeNode[index].child.size()) { ans.push_back(temp); return; } if (index >= n || !treeNode[index].child.size()) return; for (int i = 0; i < treeNode[index].child.size(); i++) { int curW = treeNode[treeNode[index].child[i]].weight; int curSum = sum + curW; if (curSum <= s) { temp.push_back(curW); dfs(treeNode[index].child[i], curSum); temp.pop_back(); } } return; }
intmain(){ cin >> n >> m >> s; for (int i = 0; i < n; i++) cin >> treeNode[i].weight; for (int i = 0; i < m; i++) { int cur, num, k; scanf("%d %d", &cur, &num); for (int j = 0; j < num; j++) { scanf("%d", &k); treeNode[cur].child.push_back(k); } sort(treeNode[cur].child.begin(), treeNode[cur].child.end(), [&](int a, int b) { return treeNode[a].weight > treeNode[b].weight; }); } temp.push_back(treeNode[0].weight); dfs(0, treeNode[0].weight);
for (int i = 0; i < ans.size(); i++) { for (int j = 0; j < ans[i].size(); j++) { cout << ans[i][j]; if (j != ans[i].size() - 1) cout << " "; } if (i != ans.size() - 1) cout << endl; } return0; }
#include<bits/stdc++.h> usingnamespace std; int n, idx = 0; vector<int> node; unordered_map<int, pair<int, int>> mp;
structTreeNode { int order; int val; TreeNode* left; TreeNode* right; };
voidinsert(TreeNode* &root){ if (mp[root->order].first >= 0) { TreeNode* l = new TreeNode; l -> order = mp[root->order].first; root -> left = l; insert(root -> left); } else { root -> left = NULL; } if (mp[root->order].second >= 0) { TreeNode* r = new TreeNode; r -> order = mp[root->order].second; root -> right = r; insert(root -> right); } else { root -> right = NULL; } return; }
voidinOrderTraversal(TreeNode* root){ if (!root) return; inOrderTraversal(root -> left); root -> val = node[idx++]; inOrderTraversal(root -> right); }
voidlevelTraversal(TreeNode* root){ if (!root) return; queue<TreeNode*> q; q.push(root); int num = 0; while(!q.empty()) { TreeNode* cur = q.front(); q.pop(); cout << cur -> val; num++; if (num < n) cout << " "; if (cur -> left) q.push(cur -> left); if (cur -> right) q.push(cur -> right); } return; }
intmain(){ cin >> n; for (int i = 0; i < n; i++) { int a,b; cin >> a >> b; mp[i] = make_pair(a, b); } for (int i = 0; i < n; i++) { int k; cin >> k; node.push_back(k); } sort(node.begin(), node.end()); TreeNode* root = new TreeNode; root -> order = 0; insert(root); inOrderTraversal(root); levelTraversal(root); return0; }