void traversal(TreeNode* cur, vector<int>& path)
{
path.push_back(cur->val);
if (cur->left == nullptr && cur->right == nullptr) {
cout << "path::::";
for (auto it : path) {
cout << it << "->" ;
}
cout << endl;
return;
}
if (cur->left) {
traversal(cur->left,path);
path.pop_back();
}
- if (cur->right) {
traversal(cur->right, path);
path.pop_back();
}
}
输出从根节点要叶子节点的所有路径
二叉树所有路径
最新推荐文章于 2024-05-03 16:02:20 发布