diff --git a/codes/cpp/chapter_backtracking/preorder_traversal_iii_compact.cpp b/codes/cpp/chapter_backtracking/preorder_traversal_iii_compact.cpp index fa44b638e..84207294e 100644 --- a/codes/cpp/chapter_backtracking/preorder_traversal_iii_compact.cpp +++ b/codes/cpp/chapter_backtracking/preorder_traversal_iii_compact.cpp @@ -20,6 +20,7 @@ void preOrder(TreeNode *root) { if (root->val == 7) { // 记录解 res.push_back(path); + path.pop_back(); return; } preOrder(root->left); diff --git a/codes/csharp/chapter_backtracking/preorder_traversal_iii_compact.cs b/codes/csharp/chapter_backtracking/preorder_traversal_iii_compact.cs index 63175ac93..731ff72fc 100644 --- a/codes/csharp/chapter_backtracking/preorder_traversal_iii_compact.cs +++ b/codes/csharp/chapter_backtracking/preorder_traversal_iii_compact.cs @@ -21,6 +21,7 @@ public class preorder_traversal_iii_compact { if (root.val == 7) { // 记录解 res.Add(new List(path)); + path.RemoveAt(path.Count - 1); return; } preOrder(root.left); @@ -40,7 +41,7 @@ public class preorder_traversal_iii_compact { res = new List>(); preOrder(root); - Console.WriteLine("\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点"); + Console.WriteLine("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点,仅包含一个值为 7 的节点"); foreach (List path in res) { PrintUtil.PrintList(path.Select(p => p.val).ToList()); } diff --git a/codes/go/chapter_backtracking/preorder_traversal_iii_compact.go b/codes/go/chapter_backtracking/preorder_traversal_iii_compact.go index 36ada5ca6..3a7b0237a 100644 --- a/codes/go/chapter_backtracking/preorder_traversal_iii_compact.go +++ b/codes/go/chapter_backtracking/preorder_traversal_iii_compact.go @@ -19,6 +19,7 @@ func preOrderIII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) { if int(root.Val) == 7 { // 记录解 *res = append(*res, *path) + *path = (*path)[:len(*path)-1] return } preOrderIII(root.Left, res, path) diff --git a/codes/go/chapter_backtracking/preorder_traversal_test.go b/codes/go/chapter_backtracking/preorder_traversal_test.go index b71d429b8..295539552 100644 --- a/codes/go/chapter_backtracking/preorder_traversal_test.go +++ b/codes/go/chapter_backtracking/preorder_traversal_test.go @@ -59,7 +59,7 @@ func TestPreorderTraversalIIICompact(t *testing.T) { res := make([][]*TreeNode, 0) preOrderIII(root, &res, &path) - fmt.Println("\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点") + fmt.Println("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点,仅包含一个值为 7 的节点") for _, path := range res { for _, node := range path { fmt.Printf("%v ", node.Val) @@ -81,7 +81,7 @@ func TestPreorderTraversalIIITemplate(t *testing.T) { choices = append(choices, root) backtrackIII(&state, &choices, &res) - fmt.Println("\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点") + fmt.Println("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点,仅包含一个值为 7 的节点") for _, path := range res { for _, node := range path { fmt.Printf("%v ", node.Val) diff --git a/codes/java/chapter_backtracking/preorder_traversal_iii_compact.java b/codes/java/chapter_backtracking/preorder_traversal_iii_compact.java index 50c3c8c29..469c6dfa9 100644 --- a/codes/java/chapter_backtracking/preorder_traversal_iii_compact.java +++ b/codes/java/chapter_backtracking/preorder_traversal_iii_compact.java @@ -24,6 +24,7 @@ public class preorder_traversal_iii_compact { if (root.val == 7) { // 记录解 res.add(new ArrayList<>(path)); + path.remove(path.size() - 1); return; } preOrder(root.left); @@ -42,7 +43,7 @@ public class preorder_traversal_iii_compact { res = new ArrayList<>(); preOrder(root); - System.out.println("\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点"); + System.out.println("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点,仅包含一个值为 7 的节点"); for (List path : res) { List vals = new ArrayList<>(); for (TreeNode node : path) { diff --git a/codes/javascript/chapter_backtracking/preorder_traversal_iii_compact.js b/codes/javascript/chapter_backtracking/preorder_traversal_iii_compact.js index 08c78b936..73cfcac85 100644 --- a/codes/javascript/chapter_backtracking/preorder_traversal_iii_compact.js +++ b/codes/javascript/chapter_backtracking/preorder_traversal_iii_compact.js @@ -18,6 +18,7 @@ function preOrder(root, path, res) { if (root.val === 7) { // 记录解 res.push([...path]); + path.pop(); return; } preOrder(root.left, path, res); @@ -36,7 +37,7 @@ const path = []; const res = []; preOrder(root, path, res); -console.log('\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点'); +console.log('\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点,仅包含一个值为 7 的节点'); res.forEach((path) => { console.log(path.map((node) => node.val)); }); diff --git a/codes/python/chapter_backtracking/preorder_traversal_iii_compact.py b/codes/python/chapter_backtracking/preorder_traversal_iii_compact.py index 6ee7307bc..566f63a25 100644 --- a/codes/python/chapter_backtracking/preorder_traversal_iii_compact.py +++ b/codes/python/chapter_backtracking/preorder_traversal_iii_compact.py @@ -20,6 +20,7 @@ def pre_order(root: TreeNode) -> None: if root.val == 7: # 记录解 res.append(list(path)) + path.pop() return pre_order(root.left) pre_order(root.right) @@ -38,6 +39,6 @@ if __name__ == "__main__": res = list[list[TreeNode]]() pre_order(root) - print("\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点") + print("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点,仅包含一个值为 7 的节点") for path in res: print([node.val for node in path]) diff --git a/codes/rust/chapter_backtracking/preorder_traversal_iii_compact.rs b/codes/rust/chapter_backtracking/preorder_traversal_iii_compact.rs index ebf895764..54755ca43 100644 --- a/codes/rust/chapter_backtracking/preorder_traversal_iii_compact.rs +++ b/codes/rust/chapter_backtracking/preorder_traversal_iii_compact.rs @@ -21,6 +21,7 @@ fn pre_order(res: &mut Vec>>>, path: &mut Vec { console.log(path.map((node) => node.val)); });