Modify the problem of preorder_traversal_iii

This commit is contained in:
krahets 2023-07-19 16:37:12 +08:00
parent 4e13755023
commit c54536d1a1
10 changed files with 15 additions and 3 deletions

View file

@ -10,7 +10,7 @@ vector<TreeNode *> path;
vector<vector<TreeNode *>> res; vector<vector<TreeNode *>> res;
/* 前序遍历:例题三 */ /* 前序遍历:例题三 */
static void preOrder(TreeNode *root) { void preOrder(TreeNode *root) {
// 剪枝 // 剪枝
if (root == nullptr || root->val == 3) { if (root == nullptr || root->val == 3) {
return; return;
@ -20,6 +20,7 @@ static void preOrder(TreeNode *root) {
if (root->val == 7) { if (root->val == 7) {
// 记录解 // 记录解
res.push_back(path); res.push_back(path);
return;
} }
preOrder(root->left); preOrder(root->left);
preOrder(root->right); preOrder(root->right);

View file

@ -21,6 +21,7 @@ public class preorder_traversal_iii_compact {
if (root.val == 7) { if (root.val == 7) {
// 记录解 // 记录解
res.Add(new List<TreeNode>(path)); res.Add(new List<TreeNode>(path));
return;
} }
preOrder(root.left); preOrder(root.left);
preOrder(root.right); preOrder(root.right);

View file

@ -19,6 +19,7 @@ func preOrderIII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {
if int(root.Val) == 7 { if int(root.Val) == 7 {
// 记录解 // 记录解
*res = append(*res, *path) *res = append(*res, *path)
return
} }
preOrderIII(root.Left, res, path) preOrderIII(root.Left, res, path)
preOrderIII(root.Right, res, path) preOrderIII(root.Right, res, path)

View file

@ -24,6 +24,7 @@ public class preorder_traversal_iii_compact {
if (root.val == 7) { if (root.val == 7) {
// 记录解 // 记录解
res.add(new ArrayList<>(path)); res.add(new ArrayList<>(path));
return;
} }
preOrder(root.left); preOrder(root.left);
preOrder(root.right); preOrder(root.right);

View file

@ -18,6 +18,7 @@ function preOrder(root, path, res) {
if (root.val === 7) { if (root.val === 7) {
// 记录解 // 记录解
res.push([...path]); res.push([...path]);
return;
} }
preOrder(root.left, path, res); preOrder(root.left, path, res);
preOrder(root.right, path, res); preOrder(root.right, path, res);

View file

@ -20,6 +20,7 @@ def pre_order(root: TreeNode) -> None:
if root.val == 7: if root.val == 7:
# 记录解 # 记录解
res.append(list(path)) res.append(list(path))
return
pre_order(root.left) pre_order(root.left)
pre_order(root.right) pre_order(root.right)
# 回退 # 回退

View file

@ -21,6 +21,7 @@ fn pre_order(res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>, path: &mut Vec<Rc<RefCel
if node.borrow().val == 7 { if node.borrow().val == 7 {
// 记录解 // 记录解
res.push(path.clone()); res.push(path.clone());
return;
} }
pre_order(res, path, node.borrow().left.clone()); pre_order(res, path, node.borrow().left.clone());
pre_order(res, path, node.borrow().right.clone()); pre_order(res, path, node.borrow().right.clone());

View file

@ -20,6 +20,7 @@ func preOrder(root: TreeNode?) {
if root.val == 7 { if root.val == 7 {
// //
res.append(path) res.append(path)
return
} }
preOrder(root: root.left) preOrder(root: root.left)
preOrder(root: root.right) preOrder(root: root.right)

View file

@ -23,6 +23,7 @@ function preOrder(
if (root.val === 7) { if (root.val === 7) {
// 记录解 // 记录解
res.push([...path]); res.push([...path]);
return;
} }
preOrder(root.left, path, res); preOrder(root.left, path, res);
preOrder(root.right, path, res); preOrder(root.right, path, res);

View file

@ -199,9 +199,12 @@
!!! question "例题三" !!! question "例题三"
在二叉树中搜索所有值为 $7$ 的节点,返回根节点到这些节点的路径,**路径中不能包含值为 $3$ 的节点**。 在二叉树中搜索所有值为 $7$ 的节点,返回根节点到这些节点的路径,**路径中有且只有一个值为 $7$ 的节点,并且不能包含值为 $3$ 的节点**。
**解题思路**:在例题二的基础上添加剪枝操作,当遇到值为 $3$ 的节点时,则终止继续搜索。 **解题思路**:在例题二的基础上添加剪枝操作。
- 当遇到值为 $7$ 的节点时,记录解并停止搜索。
- 当遇到值为 $3$ 的节点时,则终止继续搜索。
=== "Java" === "Java"