算法 - 深度优先搜索和广度优先搜索 - 深度优先 - 非递归
最编程
2024-03-24 20:39:46
...
void dfs(TreeNode *pRoot)
{
if (!pRoot) return;
stack<TreeNode*> s;
TreeNode* cur = pRoot;
while (cur || !s.empty()) {
while (cur) {
s.push(cur);
cur = cur->left;
}
cur = s.top();
cout<<cur.val;
s.pop();
cur = cur->right;
}
return true;
}
上一篇: 访谈算法-86-合并间隔-主题
推荐阅读
-
经典算法 ----- 农民过河问题(深度优先搜索)
-
LeetCode-200. [深度优先搜索广度优先搜索并发搜索集合数组矩阵] [深度优先搜索广度优先搜索并发搜索集合数组矩阵] [LeetCode-200.
-
算法 - 深度优先搜索和广度优先搜索 - 深度优先 - 非递归
-
解决三维盒子问题的启发式深度优先搜索算法(python)
-
深入讲解 ES 聚合算法的原理:深度优先算法 (DFS) 和广度优先算法 (BFS) (I)
-
[算法介绍] 图的深度优先搜索遍历(DFS)--基本思想是:从图中的一个顶点 vi 开始,访问这个顶点并标记它,然后依次搜索 vi 的每个邻居 vj;如果 vj 未被访问,则访问并标记 vj,然后依次搜索 vj 的每个邻居;如果 vj 的邻居未被访问,则访问 vj 的邻居并标记它们,直到访问完图中与 vi 有路径连接的所有顶点。如果图中仍有顶点未被访问(在无连接顶点的情况下),则选择图中另一个未被访问的顶点作为起点,重复上述过程,直到图中所有顶点都被访问。 在下面的程序中,假设图如下所示: A B C D E 对应的序号是 0 1 2 3 4。上图的轨迹是深度优先搜索遍历。
-
广度优先搜索算法(BFS)
-
蓝桥杯算法竞赛系列第 5 章 - 深度优先搜索(DFS)的提升部分
-
基于 Node.js 的地铁线路规划实现--参考广度优先搜索算法
-
LeetCode解题技巧:深度优先搜索、广度优先搜索、回溯法与剪枝 - C++实现 专题2:生成括号