chapter_backtracking/backtracking_algorithm/ #460
Replies: 29 comments 29 replies
-
非常好!很细节很通俗,支持支持! |
Beta Was this translation helpful? Give feedback.
-
k神,你好,如果为 4 节点新增一个 left 节点,值也为7,那么回溯算法无法搜索到 [1, 7, 4, 7] 的路径,但是把记录解后面的 return 语句去除就能搜索到,请问这个 return 语句的作用是什么呢? |
Beta Was this translation helpful? Give feedback.
-
/* 前序遍历:例题三 */
void preOrder(TreeNode *root) {
// 剪枝
if (root == nullptr || root->val == 3) {
return;
}
// 尝试
path.push_back(root);
if (root->val == 7) {
// 记录解
res.push_back(path);
return;
}
preOrder(root->left);
preOrder(root->right);
// 回退
path.pop_back();
}
in the code block
{
if (root->val == 7) {
// 记录解
res.push_back(path);
return;
}
} it seem have some issue if use "return" here. i use the test code. // 5
// / \
// 4 8
// / / \
// 11 13 14
// / \ / \
// 7 2 5 7
int main()
{
string line = "[5,4,8,11,null,13,14,7,2,null,null,5,7]";
TreeNode *root = stringToTreeNode(line);
Solution* slt = new Solution();
slt->preOrder(root);
vector<vector<int>> output = slt->res;
} the result: 5->4->11->7 5->4->8->14->7 |
Beta Was this translation helpful? Give feedback.
-
通俗易懂,例子也举的好,框架实际应用前序遍历的例子太妙了,就喜欢看这种有实际实现例子的方法论 |
Beta Was this translation helpful? Give feedback.
-
怎么理解回溯和递归的关系? |
Beta Was this translation helpful? Give feedback.
-
大神真的努力教会 麻瓜使用魔法。 |
Beta Was this translation helpful? Give feedback.
-
python里,恢复状态 |
Beta Was this translation helpful? Give feedback.
-
k神,有一个问题想请教下。
回溯框架里的choices,针对树结构的choices怎么处理呀?等价于树结构前序遍历生成的数组吗? |
Beta Was this translation helpful? Give feedback.
-
上学的时候就从这里开始就搞不动了,现在好像还是差不多, 前面那一堆都花个两三天时间就搞定 |
Beta Was this translation helpful? Give feedback.
-
for choice in choices:
# 剪枝:判断选择是否合法
if is_valid(state, choice):
# 尝试:做出选择,更新状态
make_choice(state, choice)
backtrack(state, choices, res)
# 回退:撤销选择,恢复到之前的状态
undo_choice(state, choice) backtrack(state, choices, res) |
Beta Was this translation helpful? Give feedback.
-
博主,例题二、三的 go 代码中,将 path 添加到 res 这一行代码需要调整下,因为 go 的切片是一个指针指向底层数组,可能会造成数据错误。博主可以在样例树为值为 6 的节点添加一个值为 7 的左儿子节点测试看看。
|
Beta Was this translation helpful? Give feedback.
-
作者你好,我有点不理解,对例题三进行框架代码的运行时,backtrack第一次运行的时候choices是[1],那为什么可视化运行那里choices指向的是[0]这个列表啊,不应该是[1]吗 |
Beta Was this translation helpful? Give feedback.
-
如果对回溯难以理解,说明递归没有真正完全掌握,建议先复习高中的数学归纳法,并善用VS、CLion等IDE的并行堆栈、并行监视功能,他们都是帮助理解递归和回溯的优秀工具。 |
Beta Was this translation helpful? Give feedback.
-
回溯典型例题,哪里可以刷这些例题嘛,回溯算法还是比较薄弱 |
Beta Was this translation helpful? Give feedback.
-
树的回溯算法我能理解 但是数组的回溯该怎么理解 |
Beta Was this translation helpful? Give feedback.
-
我觉得树可视化还是有点问题,比如当左子树构建完成后会立即构建右子树,而不是先返回根节点 |
Beta Was this translation helpful? Give feedback.
-
献丑下二叉树的前序遍历算法: def pre_order(root: TreeNode):
return [] if not root else [root.val] + pre_order(root.left) + pre_order(root.right) |
Beta Was this translation helpful? Give feedback.
-
在剪枝中 res.append(list(path)) 这句话一定要加上list,不加上list的话,这个path只是一个引用,最后是不会输出的,只有list(path)才是将其转为path的副本 |
Beta Was this translation helpful? Give feedback.
-
请问makeChoice()改成completeChoice()会不会更好一点 |
Beta Was this translation helpful? Give feedback.
-
// 安全地处理 left 和 right 子节点
let mut next_choices = Vec::new();
if let Some(left) = choice.borrow().left.clone() {
next_choices.push(left);
}
if let Some(right) = choice.borrow().right.clone() {
next_choices.push(right);
}
// 进行下一轮选择
backtrack(state, &mut next_choices, res); |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
回溯算法就像是走迷宫,我们需要在迷宫中记录所有从迷宫入口到有金币的小道的路径时的路径规划方法一样:
在尝试了所有的路口后,所有可能已尝试完毕。 |
Beta Was this translation helpful? Give feedback.
-
框架代码中的这个部分 /* 回溯算法:例题三 */
void backtrack(TreeNode *choices[2]) {
//code...
} 其中 |
Beta Was this translation helpful? Give feedback.
-
原文列举了汉诺塔作为回溯经典例题,想请教这就是个单纯的递归问题,怎么去用回溯法呢?一直没想出来。 |
Beta Was this translation helpful? Give feedback.
-
first day |
Beta Was this translation helpful? Give feedback.
-
模板方法提炼的不错,可以变成类库了 |
Beta Was this translation helpful? Give feedback.
-
chapter_backtracking/backtracking_algorithm/
一本动画图解、能运行、可提问的数据结构与算法入门书
https://www.hello-algo.com/chapter_backtracking/backtracking_algorithm/
Beta Was this translation helpful? Give feedback.
All reactions