Add cpp code for the backtrack algorithm.

pull/462/head
krahets 2 years ago
parent 1600ed6dee
commit f5b8978330

@ -0,0 +1,75 @@
/**
* File: backtrack_find_constrained_paths.cpp
* Created Time: 2023-04-16
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
/* 判断当前状态是否为解 */
bool isSolution(vector<TreeNode *> &state) {
return !state.empty() && state.back()->val == 7;
}
/* 记录解 */
void recordSolution(vector<TreeNode *> &state, vector<vector<TreeNode *>> &res) {
res.push_back(state);
}
/* 判断在当前状态下,该选择是否合法 */
bool isValid(vector<TreeNode *> &state, TreeNode *choice) {
return choice != nullptr && choice->val != 3;
}
/* 更新状态 */
void makeChoice(vector<TreeNode *> &state, TreeNode *choice) {
state.push_back(choice);
}
/* 恢复状态 */
void undoChoice(vector<TreeNode *> &state, TreeNode *choice) {
state.pop_back();
}
/* 回溯算法 */
void backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<vector<TreeNode *>> &res) {
// 检查是否为解
if (isSolution(state)) {
// 记录解
recordSolution(state, res);
return;
}
// 遍历所有选择
for (TreeNode *choice : choices) {
// 剪枝:检查选择是否合法
if (isValid(state, choice)) {
// 尝试:做出选择,更新状态
makeChoice(state, choice);
vector<TreeNode *> nextChoices{choice->left, choice->right};
backtrack(state, nextChoices, res);
// 回退:撤销选择,恢复到之前的状态
undoChoice(state, choice);
}
}
}
int main() {
TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
cout << "\n初始化二叉树" << endl;
printTree(root);
// 回溯算法
vector<TreeNode *> state;
vector<TreeNode *> choices = {root};
vector<vector<TreeNode *>> res;
backtrack(state, choices, res);
cout << "\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点" << endl;
for (vector<TreeNode *> &path : res) {
vector<int> vals;
for (TreeNode *node : path) {
vals.push_back(node->val);
}
printVector(vals);
}
}

@ -0,0 +1,45 @@
/**
* File: preorder_find_constrained_paths.cpp
* Created Time: 2023-04-16
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
vector<TreeNode *> path;
vector<vector<TreeNode *>> res;
/* 前序遍历 */
static void preOrder(TreeNode *root) {
if (root == nullptr || root->val == 3) {
return;
}
// 尝试
path.push_back(root);
if (root->val == 7) {
// 记录解
res.push_back(path);
}
preOrder(root->left);
preOrder(root->right);
// 回退
path.pop_back();
}
int main() {
TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
cout << "\n初始化二叉树" << endl;
printTree(root);
// 前序遍历
preOrder(root);
cout << "\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点" << endl;
for (vector<TreeNode *> &path : res) {
vector<int> vals;
for (TreeNode *node : path) {
vals.push_back(node->val);
}
printVector(vals);
}
}

@ -0,0 +1,38 @@
/**
* File: preorder_find_nodes.cpp
* Created Time: 2023-04-16
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
vector<TreeNode *> res;
/* 前序遍历 */
static void preOrder(TreeNode *root) {
if (root == nullptr) {
return;
}
if (root->val == 7) {
// 记录解
res.push_back(root);
}
preOrder(root->left);
preOrder(root->right);
}
int main() {
TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
cout << "\n初始化二叉树" << endl;
printTree(root);
// 前序遍历
preOrder(root);
cout << "\n输出所有值为 7 的节点" << endl;
vector<int> vals;
for (TreeNode *node : res) {
vals.push_back(node->val);
}
printVector(vals);
}

@ -0,0 +1,45 @@
/**
* File: preorder_find_paths.cpp
* Created Time: 2023-04-16
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
vector<TreeNode *> path;
vector<vector<TreeNode *>> res;
/* 前序遍历 */
static void preOrder(TreeNode *root) {
if (root == nullptr) {
return;
}
// 尝试
path.push_back(root);
if (root->val == 7) {
// 记录解
res.push_back(path);
}
preOrder(root->left);
preOrder(root->right);
// 回退
path.pop_back();
}
int main() {
TreeNode *root = vecToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
cout << "\n初始化二叉树" << endl;
printTree(root);
// 前序遍历
preOrder(root);
cout << "\n输出所有根节点到节点 7 的路径" << endl;
for (vector<TreeNode *> &path : res) {
vector<int> vals;
for (TreeNode *node : path) {
vals.push_back(node->val);
}
printVector(vals);
}
}

@ -32,7 +32,7 @@ public class preorder_find_nodes {
res = new ArrayList<>();
preOrder(root);
System.out.println("\n输出所有根节点到节点 7 的路径");
System.out.println("\n输出所有值为 7 的节点");
List<Integer> vals = new ArrayList<>();
for (TreeNode node : res) {
vals.add(node.val);

Loading…
Cancel
Save