diff --git a/codes/cpp/chapter_backtracking/preorder_traversal_iii_compact.cpp b/codes/cpp/chapter_backtracking/preorder_traversal_iii_compact.cpp index ee00f47f5..fa44b638e 100644 --- a/codes/cpp/chapter_backtracking/preorder_traversal_iii_compact.cpp +++ b/codes/cpp/chapter_backtracking/preorder_traversal_iii_compact.cpp @@ -10,7 +10,7 @@ vector path; vector> res; /* 前序遍历:例题三 */ -static void preOrder(TreeNode *root) { +void preOrder(TreeNode *root) { // 剪枝 if (root == nullptr || root->val == 3) { return; @@ -20,6 +20,7 @@ static void preOrder(TreeNode *root) { if (root->val == 7) { // 记录解 res.push_back(path); + return; } preOrder(root->left); preOrder(root->right); diff --git a/codes/csharp/chapter_backtracking/preorder_traversal_iii_compact.cs b/codes/csharp/chapter_backtracking/preorder_traversal_iii_compact.cs index c17a6dd20..63175ac93 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)); + return; } preOrder(root.left); preOrder(root.right); diff --git a/codes/go/chapter_backtracking/preorder_traversal_iii_compact.go b/codes/go/chapter_backtracking/preorder_traversal_iii_compact.go index fcf1b86b2..36ada5ca6 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) + return } preOrderIII(root.Left, res, path) preOrderIII(root.Right, res, path) diff --git a/codes/java/chapter_backtracking/preorder_traversal_iii_compact.java b/codes/java/chapter_backtracking/preorder_traversal_iii_compact.java index 4ffbb6217..50c3c8c29 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)); + return; } preOrder(root.left); preOrder(root.right); diff --git a/codes/javascript/chapter_backtracking/preorder_traversal_iii_compact.js b/codes/javascript/chapter_backtracking/preorder_traversal_iii_compact.js index d83b7b053..08c78b936 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]); + return; } preOrder(root.left, path, res); preOrder(root.right, path, res); diff --git a/codes/python/chapter_backtracking/preorder_traversal_iii_compact.py b/codes/python/chapter_backtracking/preorder_traversal_iii_compact.py index 55bf24ee6..6ee7307bc 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)) + return pre_order(root.left) pre_order(root.right) # 回退 diff --git a/codes/rust/chapter_backtracking/preorder_traversal_iii_compact.rs b/codes/rust/chapter_backtracking/preorder_traversal_iii_compact.rs index eaa046f2d..ebf895764 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