77 lines
2.0 KiB
C++
77 lines
2.0 KiB
C++
|
/**
|
|||
|
* File: preorder_traversal_iii_template.cpp
|
|||
|
* Created Time: 2023-04-16
|
|||
|
* Author: krahets (krahets@163.com)
|
|||
|
*/
|
|||
|
|
|||
|
#include "../utils/common.hpp"
|
|||
|
|
|||
|
/* <20>жϵ<D0B6>ǰ״̬<D7B4>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
|
|||
|
bool isSolution(vector<TreeNode *> &state) {
|
|||
|
return !state.empty() && state.back()->val == 7;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><>¼<EFBFBD><C2BC> */
|
|||
|
void recordSolution(vector<TreeNode *> &state, vector<vector<TreeNode *>> &res) {
|
|||
|
res.push_back(state);
|
|||
|
}
|
|||
|
|
|||
|
/* <20>ж<EFBFBD><D0B6>ڵ<EFBFBD>ǰ״̬<D7B4>£<EFBFBD><C2A3><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD>Ƿ<EFBFBD><C7B7>Ϸ<EFBFBD> */
|
|||
|
bool isValid(vector<TreeNode *> &state, TreeNode *choice) {
|
|||
|
return choice != nullptr && choice->val != 3;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD>״̬ */
|
|||
|
void makeChoice(vector<TreeNode *> &state, TreeNode *choice) {
|
|||
|
state.push_back(choice);
|
|||
|
}
|
|||
|
|
|||
|
/* <20>ָ<EFBFBD>״̬ */
|
|||
|
void undoChoice(vector<TreeNode *> &state, TreeNode *choice) {
|
|||
|
state.pop_back();
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>㷨<EFBFBD><E3B7A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
void backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<vector<TreeNode *>> &res) {
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA>
|
|||
|
if (isSolution(state)) {
|
|||
|
// <20><>¼<EFBFBD><C2BC>
|
|||
|
recordSolution(state, res);
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1>
|
|||
|
for (TreeNode *choice : choices) {
|
|||
|
// <20><>֦<EFBFBD><D6A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD>Ƿ<EFBFBD><C7B7>Ϸ<EFBFBD>
|
|||
|
if (isValid(state, choice)) {
|
|||
|
// <20><><EFBFBD>ԣ<EFBFBD><D4A3><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD><F1A3ACB8><EFBFBD>״̬
|
|||
|
makeChoice(state, choice);
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ѡ<EFBFBD><D1A1>
|
|||
|
vector<TreeNode *> nextChoices{choice->left, choice->right};
|
|||
|
backtrack(state, nextChoices, res);
|
|||
|
// <20><><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD>ָ<F1A3ACBB><D6B8><EFBFBD>֮ǰ<D6AE><C7B0>״̬
|
|||
|
undoChoice(state, choice);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/* Driver Code */
|
|||
|
int main() {
|
|||
|
TreeNode *root = vectorToTree(vector<int>{1, 7, 3, 4, 5, 6, 7});
|
|||
|
cout << "\n<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl;
|
|||
|
printTree(root);
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>㷨
|
|||
|
vector<TreeNode *> state;
|
|||
|
vector<TreeNode *> choices = {root};
|
|||
|
vector<vector<TreeNode *>> res;
|
|||
|
backtrack(state, choices, res);
|
|||
|
|
|||
|
cout << "\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>и<EFBFBD><EFBFBD>ڵ㵽<EFBFBD>ڵ<EFBFBD> 7 <20><>·<EFBFBD><C2B7><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>·<EFBFBD><C2B7><EFBFBD>в<EFBFBD><D0B2><EFBFBD><EFBFBD><EFBFBD>ֵΪ 3 <20>Ľڵ<C4BD>" << endl;
|
|||
|
for (vector<TreeNode *> &path : res) {
|
|||
|
vector<int> vals;
|
|||
|
for (TreeNode *node : path) {
|
|||
|
vals.push_back(node->val);
|
|||
|
}
|
|||
|
printVector(vals);
|
|||
|
}
|
|||
|
}
|