mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 03:30:30 +08:00
Sort the coding languages by applications. (#721)
This commit is contained in:
@@ -10,10 +10,10 @@
|
||||
|
||||
对于此题,我们前序遍历这颗树,并判断当前节点的值是否为 $7$ ,若是则将该节点的值加入到结果列表 `res` 之中。相关过程实现如下图和以下代码所示。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title="preorder_traversal_i_compact.java"
|
||||
[class]{preorder_traversal_i_compact}-[func]{preOrder}
|
||||
```python title="preorder_traversal_i_compact.py"
|
||||
[class]{}-[func]{pre_order}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -22,10 +22,16 @@
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title="preorder_traversal_i_compact.py"
|
||||
[class]{}-[func]{pre_order}
|
||||
```java title="preorder_traversal_i_compact.java"
|
||||
[class]{preorder_traversal_i_compact}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="preorder_traversal_i_compact.cs"
|
||||
[class]{preorder_traversal_i_compact}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -34,6 +40,12 @@
|
||||
[class]{}-[func]{preOrderI}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="preorder_traversal_i_compact.swift"
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="preorder_traversal_i_compact.js"
|
||||
@@ -46,30 +58,6 @@
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="preorder_traversal_i_compact.c"
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="preorder_traversal_i_compact.cs"
|
||||
[class]{preorder_traversal_i_compact}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="preorder_traversal_i_compact.swift"
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="preorder_traversal_i_compact.zig"
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="preorder_traversal_i_compact.dart"
|
||||
@@ -82,6 +70,18 @@
|
||||
[class]{}-[func]{pre_order}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="preorder_traversal_i_compact.c"
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="preorder_traversal_i_compact.zig"
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||

|
||||
|
||||
## 尝试与回退
|
||||
@@ -98,10 +98,10 @@
|
||||
|
||||
在例题一代码的基础上,我们需要借助一个列表 `path` 记录访问过的节点路径。当访问到值为 $7$ 的节点时,则复制 `path` 并添加进结果列表 `res` 。遍历完成后,`res` 中保存的就是所有的解。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title="preorder_traversal_ii_compact.java"
|
||||
[class]{preorder_traversal_ii_compact}-[func]{preOrder}
|
||||
```python title="preorder_traversal_ii_compact.py"
|
||||
[class]{}-[func]{pre_order}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -110,10 +110,16 @@
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title="preorder_traversal_ii_compact.py"
|
||||
[class]{}-[func]{pre_order}
|
||||
```java title="preorder_traversal_ii_compact.java"
|
||||
[class]{preorder_traversal_ii_compact}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="preorder_traversal_ii_compact.cs"
|
||||
[class]{preorder_traversal_ii_compact}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -122,6 +128,12 @@
|
||||
[class]{}-[func]{preOrderII}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="preorder_traversal_ii_compact.swift"
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="preorder_traversal_ii_compact.js"
|
||||
@@ -134,30 +146,6 @@
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="preorder_traversal_ii_compact.c"
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="preorder_traversal_ii_compact.cs"
|
||||
[class]{preorder_traversal_ii_compact}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="preorder_traversal_ii_compact.swift"
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="preorder_traversal_ii_compact.zig"
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="preorder_traversal_ii_compact.dart"
|
||||
@@ -170,6 +158,18 @@
|
||||
[class]{}-[func]{pre_order}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="preorder_traversal_ii_compact.c"
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="preorder_traversal_ii_compact.zig"
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
在每次“尝试”中,我们通过将当前节点添加进 `path` 来记录路径;而在“回退”前,我们需要将该节点从 `path` 中弹出,**以恢复本次尝试之前的状态**。
|
||||
|
||||
观察下图所示的过程,**我们可以将尝试和回退理解为“前进”与“撤销”**,两个操作是互为逆向的。
|
||||
@@ -217,10 +217,10 @@
|
||||
|
||||
为了满足以上约束条件,**我们需要添加剪枝操作**:在搜索过程中,若遇到值为 $3$ 的节点,则提前返回,停止继续搜索。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title="preorder_traversal_iii_compact.java"
|
||||
[class]{preorder_traversal_iii_compact}-[func]{preOrder}
|
||||
```python title="preorder_traversal_iii_compact.py"
|
||||
[class]{}-[func]{pre_order}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -229,10 +229,16 @@
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title="preorder_traversal_iii_compact.py"
|
||||
[class]{}-[func]{pre_order}
|
||||
```java title="preorder_traversal_iii_compact.java"
|
||||
[class]{preorder_traversal_iii_compact}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="preorder_traversal_iii_compact.cs"
|
||||
[class]{preorder_traversal_iii_compact}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -241,6 +247,12 @@
|
||||
[class]{}-[func]{preOrderIII}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="preorder_traversal_iii_compact.swift"
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="preorder_traversal_iii_compact.js"
|
||||
@@ -253,30 +265,6 @@
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="preorder_traversal_iii_compact.c"
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="preorder_traversal_iii_compact.cs"
|
||||
[class]{preorder_traversal_iii_compact}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="preorder_traversal_iii_compact.swift"
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="preorder_traversal_iii_compact.zig"
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="preorder_traversal_iii_compact.dart"
|
||||
@@ -289,6 +277,18 @@
|
||||
[class]{}-[func]{pre_order}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="preorder_traversal_iii_compact.c"
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="preorder_traversal_iii_compact.zig"
|
||||
[class]{}-[func]{preOrder}
|
||||
```
|
||||
|
||||
剪枝是一个非常形象的名词。如下图所示,在搜索过程中,**我们“剪掉”了不满足约束条件的搜索分支**,避免许多无意义的尝试,从而提高了搜索效率。
|
||||
|
||||

|
||||
@@ -299,30 +299,26 @@
|
||||
|
||||
在以下框架代码中,`state` 表示问题的当前状态,`choices` 表示当前状态下可以做出的选择。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title=""
|
||||
/* 回溯算法框架 */
|
||||
void backtrack(State state, List<Choice> choices, List<State> res) {
|
||||
// 判断是否为解
|
||||
if (isSolution(state)) {
|
||||
// 记录解
|
||||
recordSolution(state, res);
|
||||
// 停止继续搜索
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (Choice choice : choices) {
|
||||
// 剪枝:判断选择是否合法
|
||||
if (isValid(state, choice)) {
|
||||
// 尝试:做出选择,更新状态
|
||||
makeChoice(state, choice);
|
||||
backtrack(state, choices, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
undoChoice(state, choice);
|
||||
}
|
||||
}
|
||||
}
|
||||
```python title=""
|
||||
def backtrack(state: State, choices: list[choice], res: list[state]):
|
||||
"""回溯算法框架"""
|
||||
# 判断是否为解
|
||||
if is_solution(state):
|
||||
# 记录解
|
||||
record_solution(state, res)
|
||||
# 停止继续搜索
|
||||
return
|
||||
# 遍历所有选择
|
||||
for choice in choices:
|
||||
# 剪枝:判断选择是否合法
|
||||
if is_valid(state, choice):
|
||||
# 尝试:做出选择,更新状态
|
||||
make_choice(state, choice)
|
||||
backtrack(state, choices, res)
|
||||
# 回退:撤销选择,恢复到之前的状态
|
||||
undo_choice(state, choice)
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -351,26 +347,56 @@
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title=""
|
||||
def backtrack(state: State, choices: list[choice], res: list[state]):
|
||||
"""回溯算法框架"""
|
||||
# 判断是否为解
|
||||
if is_solution(state):
|
||||
# 记录解
|
||||
record_solution(state, res)
|
||||
# 停止继续搜索
|
||||
return
|
||||
# 遍历所有选择
|
||||
for choice in choices:
|
||||
# 剪枝:判断选择是否合法
|
||||
if is_valid(state, choice):
|
||||
# 尝试:做出选择,更新状态
|
||||
make_choice(state, choice)
|
||||
backtrack(state, choices, res)
|
||||
# 回退:撤销选择,恢复到之前的状态
|
||||
undo_choice(state, choice)
|
||||
```java title=""
|
||||
/* 回溯算法框架 */
|
||||
void backtrack(State state, List<Choice> choices, List<State> res) {
|
||||
// 判断是否为解
|
||||
if (isSolution(state)) {
|
||||
// 记录解
|
||||
recordSolution(state, res);
|
||||
// 停止继续搜索
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (Choice choice : choices) {
|
||||
// 剪枝:判断选择是否合法
|
||||
if (isValid(state, choice)) {
|
||||
// 尝试:做出选择,更新状态
|
||||
makeChoice(state, choice);
|
||||
backtrack(state, choices, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
undoChoice(state, choice);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
/* 回溯算法框架 */
|
||||
void backtrack(State state, List<Choice> choices, List<State> res) {
|
||||
// 判断是否为解
|
||||
if (isSolution(state)) {
|
||||
// 记录解
|
||||
recordSolution(state, res);
|
||||
// 停止继续搜索
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
foreach (Choice choice in choices) {
|
||||
// 剪枝:判断选择是否合法
|
||||
if (isValid(state, choice)) {
|
||||
// 尝试:做出选择,更新状态
|
||||
makeChoice(state, choice);
|
||||
backtrack(state, choices, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
undoChoice(state, choice);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -399,6 +425,32 @@
|
||||
}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title=""
|
||||
/* 回溯算法框架 */
|
||||
func backtrack(state: inout State, choices: [Choice], res: inout [State]) {
|
||||
// 判断是否为解
|
||||
if isSolution(state: state) {
|
||||
// 记录解
|
||||
recordSolution(state: state, res: &res)
|
||||
// 停止继续搜索
|
||||
return
|
||||
}
|
||||
// 遍历所有选择
|
||||
for choice in choices {
|
||||
// 剪枝:判断选择是否合法
|
||||
if isValid(state: state, choice: choice) {
|
||||
// 尝试:做出选择,更新状态
|
||||
makeChoice(state: &state, choice: choice)
|
||||
backtrack(state: &state, choices: choices, res: &res)
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
undoChoice(state: &state, choice: choice)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title=""
|
||||
@@ -451,90 +503,6 @@
|
||||
}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title=""
|
||||
/* 回溯算法框架 */
|
||||
void backtrack(State *state, Choice *choices, int numChoices, State *res, int numRes) {
|
||||
// 判断是否为解
|
||||
if (isSolution(state)) {
|
||||
// 记录解
|
||||
recordSolution(state, res, numRes);
|
||||
// 停止继续搜索
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (int i = 0; i < numChoices; i++) {
|
||||
// 剪枝:判断选择是否合法
|
||||
if (isValid(state, &choices[i])) {
|
||||
// 尝试:做出选择,更新状态
|
||||
makeChoice(state, &choices[i]);
|
||||
backtrack(state, choices, numChoices, res, numRes);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
undoChoice(state, &choices[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
/* 回溯算法框架 */
|
||||
void backtrack(State state, List<Choice> choices, List<State> res) {
|
||||
// 判断是否为解
|
||||
if (isSolution(state)) {
|
||||
// 记录解
|
||||
recordSolution(state, res);
|
||||
// 停止继续搜索
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
foreach (Choice choice in choices) {
|
||||
// 剪枝:判断选择是否合法
|
||||
if (isValid(state, choice)) {
|
||||
// 尝试:做出选择,更新状态
|
||||
makeChoice(state, choice);
|
||||
backtrack(state, choices, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
undoChoice(state, choice);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title=""
|
||||
/* 回溯算法框架 */
|
||||
func backtrack(state: inout State, choices: [Choice], res: inout [State]) {
|
||||
// 判断是否为解
|
||||
if isSolution(state: state) {
|
||||
// 记录解
|
||||
recordSolution(state: state, res: &res)
|
||||
// 停止继续搜索
|
||||
return
|
||||
}
|
||||
// 遍历所有选择
|
||||
for choice in choices {
|
||||
// 剪枝:判断选择是否合法
|
||||
if isValid(state: state, choice: choice) {
|
||||
// 尝试:做出选择,更新状态
|
||||
makeChoice(state: &state, choice: choice)
|
||||
backtrack(state: &state, choices: choices, res: &res)
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
undoChoice(state: &state, choice: choice)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title=""
|
||||
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title=""
|
||||
@@ -567,22 +535,54 @@
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title=""
|
||||
/* 回溯算法框架 */
|
||||
void backtrack(State *state, Choice *choices, int numChoices, State *res, int numRes) {
|
||||
// 判断是否为解
|
||||
if (isSolution(state)) {
|
||||
// 记录解
|
||||
recordSolution(state, res, numRes);
|
||||
// 停止继续搜索
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (int i = 0; i < numChoices; i++) {
|
||||
// 剪枝:判断选择是否合法
|
||||
if (isValid(state, &choices[i])) {
|
||||
// 尝试:做出选择,更新状态
|
||||
makeChoice(state, &choices[i]);
|
||||
backtrack(state, choices, numChoices, res, numRes);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
undoChoice(state, &choices[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title=""
|
||||
|
||||
```
|
||||
|
||||
接下来,我们基于框架代码来解决例题三。状态 `state` 为节点遍历路径,选择 `choices` 为当前节点的左子节点和右子节点,结果 `res` 是路径列表。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title="preorder_traversal_iii_template.java"
|
||||
[class]{preorder_traversal_iii_template}-[func]{isSolution}
|
||||
```python title="preorder_traversal_iii_template.py"
|
||||
[class]{}-[func]{is_solution}
|
||||
|
||||
[class]{preorder_traversal_iii_template}-[func]{recordSolution}
|
||||
[class]{}-[func]{record_solution}
|
||||
|
||||
[class]{preorder_traversal_iii_template}-[func]{isValid}
|
||||
[class]{}-[func]{is_valid}
|
||||
|
||||
[class]{preorder_traversal_iii_template}-[func]{makeChoice}
|
||||
[class]{}-[func]{make_choice}
|
||||
|
||||
[class]{preorder_traversal_iii_template}-[func]{undoChoice}
|
||||
[class]{}-[func]{undo_choice}
|
||||
|
||||
[class]{preorder_traversal_iii_template}-[func]{backtrack}
|
||||
[class]{}-[func]{backtrack}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -601,20 +601,36 @@
|
||||
[class]{}-[func]{backtrack}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title="preorder_traversal_iii_template.py"
|
||||
[class]{}-[func]{is_solution}
|
||||
```java title="preorder_traversal_iii_template.java"
|
||||
[class]{preorder_traversal_iii_template}-[func]{isSolution}
|
||||
|
||||
[class]{}-[func]{record_solution}
|
||||
[class]{preorder_traversal_iii_template}-[func]{recordSolution}
|
||||
|
||||
[class]{}-[func]{is_valid}
|
||||
[class]{preorder_traversal_iii_template}-[func]{isValid}
|
||||
|
||||
[class]{}-[func]{make_choice}
|
||||
[class]{preorder_traversal_iii_template}-[func]{makeChoice}
|
||||
|
||||
[class]{}-[func]{undo_choice}
|
||||
[class]{preorder_traversal_iii_template}-[func]{undoChoice}
|
||||
|
||||
[class]{}-[func]{backtrack}
|
||||
[class]{preorder_traversal_iii_template}-[func]{backtrack}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="preorder_traversal_iii_template.cs"
|
||||
[class]{preorder_traversal_iii_template}-[func]{isSolution}
|
||||
|
||||
[class]{preorder_traversal_iii_template}-[func]{recordSolution}
|
||||
|
||||
[class]{preorder_traversal_iii_template}-[func]{isValid}
|
||||
|
||||
[class]{preorder_traversal_iii_template}-[func]{makeChoice}
|
||||
|
||||
[class]{preorder_traversal_iii_template}-[func]{undoChoice}
|
||||
|
||||
[class]{preorder_traversal_iii_template}-[func]{backtrack}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -633,6 +649,22 @@
|
||||
[class]{}-[func]{backtrackIII}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="preorder_traversal_iii_template.swift"
|
||||
[class]{}-[func]{isSolution}
|
||||
|
||||
[class]{}-[func]{recordSolution}
|
||||
|
||||
[class]{}-[func]{isValid}
|
||||
|
||||
[class]{}-[func]{makeChoice}
|
||||
|
||||
[class]{}-[func]{undoChoice}
|
||||
|
||||
[class]{}-[func]{backtrack}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="preorder_traversal_iii_template.js"
|
||||
@@ -665,70 +697,6 @@
|
||||
[class]{}-[func]{backtrack}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="preorder_traversal_iii_template.c"
|
||||
[class]{}-[func]{isSolution}
|
||||
|
||||
[class]{}-[func]{recordSolution}
|
||||
|
||||
[class]{}-[func]{isValid}
|
||||
|
||||
[class]{}-[func]{makeChoice}
|
||||
|
||||
[class]{}-[func]{undoChoice}
|
||||
|
||||
[class]{}-[func]{backtrack}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="preorder_traversal_iii_template.cs"
|
||||
[class]{preorder_traversal_iii_template}-[func]{isSolution}
|
||||
|
||||
[class]{preorder_traversal_iii_template}-[func]{recordSolution}
|
||||
|
||||
[class]{preorder_traversal_iii_template}-[func]{isValid}
|
||||
|
||||
[class]{preorder_traversal_iii_template}-[func]{makeChoice}
|
||||
|
||||
[class]{preorder_traversal_iii_template}-[func]{undoChoice}
|
||||
|
||||
[class]{preorder_traversal_iii_template}-[func]{backtrack}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="preorder_traversal_iii_template.swift"
|
||||
[class]{}-[func]{isSolution}
|
||||
|
||||
[class]{}-[func]{recordSolution}
|
||||
|
||||
[class]{}-[func]{isValid}
|
||||
|
||||
[class]{}-[func]{makeChoice}
|
||||
|
||||
[class]{}-[func]{undoChoice}
|
||||
|
||||
[class]{}-[func]{backtrack}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="preorder_traversal_iii_template.zig"
|
||||
[class]{}-[func]{isSolution}
|
||||
|
||||
[class]{}-[func]{recordSolution}
|
||||
|
||||
[class]{}-[func]{isValid}
|
||||
|
||||
[class]{}-[func]{makeChoice}
|
||||
|
||||
[class]{}-[func]{undoChoice}
|
||||
|
||||
[class]{}-[func]{backtrack}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="preorder_traversal_iii_template.dart"
|
||||
@@ -761,6 +729,38 @@
|
||||
[class]{}-[func]{backtrack}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="preorder_traversal_iii_template.c"
|
||||
[class]{}-[func]{isSolution}
|
||||
|
||||
[class]{}-[func]{recordSolution}
|
||||
|
||||
[class]{}-[func]{isValid}
|
||||
|
||||
[class]{}-[func]{makeChoice}
|
||||
|
||||
[class]{}-[func]{undoChoice}
|
||||
|
||||
[class]{}-[func]{backtrack}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="preorder_traversal_iii_template.zig"
|
||||
[class]{}-[func]{isSolution}
|
||||
|
||||
[class]{}-[func]{recordSolution}
|
||||
|
||||
[class]{}-[func]{isValid}
|
||||
|
||||
[class]{}-[func]{makeChoice}
|
||||
|
||||
[class]{}-[func]{undoChoice}
|
||||
|
||||
[class]{}-[func]{backtrack}
|
||||
```
|
||||
|
||||
根据题意,我们在找到值为 7 的节点后应该继续搜索,**因此需要将记录解之后的 `return` 语句删除**。下图对比了保留或删除 `return` 语句的搜索过程。
|
||||
|
||||

|
||||
|
||||
Reference in New Issue
Block a user