Skip to content

Commit

Permalink
Update 二叉树中递归带着回溯.md
Browse files Browse the repository at this point in the history
进一步探讨左右子树情况
  • Loading branch information
casnz1601 authored Oct 4, 2021
1 parent 87f9a26 commit 7b6fd76
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions problems/二叉树中递归带着回溯.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,22 @@ if (cur->right) {
}
```
此时就没有回溯了,这个代码就是通过不了的了
因为在递归右子树之前需要还原path,所以在左子树递归后必须为了右子树而进行回溯操作。而只右子树自己不添加回溯也可以成功AC
如果想把回溯加上,就要 在上面代码的基础上,加上回溯,就可以AC了。
因此,在上面代码的基础上,再加上左右子树的回溯代码,就可以AC了。
```CPP
if (cur->left) {
path += "->";
traversal(cur->left, path, result); // 左
path.pop_back(); // 回溯
path.pop_back();
path.pop_back(); // 回溯,抛掉val
path.pop_back(); // 回溯,抛掉->
}
if (cur->right) {
path += "->";
traversal(cur->right, path, result); // 右
path.pop_back(); // 回溯
path.pop_back();
path.pop_back(); // 回溯(非必要)
path.pop_back();
}
```

Expand Down

0 comments on commit 7b6fd76

Please sign in to comment.