mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-27 16:16:28 +08:00
a005c6ebd3
* Update avatar's link in the landing page * Bug fixes * Move assets folder from overrides to docs * Reduce figures' corner radius * Update copyright * Update header image * Krahets -> krahets * Update the landing page
46 lines
998 B
C++
46 lines
998 B
C++
/**
|
|
* File: preorder_traversal_ii_compact.cpp
|
|
* Created Time: 2023-04-16
|
|
* Author: krahets (krahets@163.com)
|
|
*/
|
|
|
|
#include "../utils/common.hpp"
|
|
|
|
vector<TreeNode *> path;
|
|
vector<vector<TreeNode *>> res;
|
|
|
|
/* 前序遍历:例题二 */
|
|
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();
|
|
}
|
|
|
|
/* Driver Code */
|
|
int main() {
|
|
TreeNode *root = vectorToTree(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);
|
|
}
|
|
}
|