mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 01:16:31 +08:00
Modify the problem of preorder_traversal_iii
This commit is contained in:
parent
4e13755023
commit
c54536d1a1
10 changed files with 15 additions and 3 deletions
|
@ -10,7 +10,7 @@ vector<TreeNode *> path;
|
|||
vector<vector<TreeNode *>> 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);
|
||||
|
|
|
@ -21,6 +21,7 @@ public class preorder_traversal_iii_compact {
|
|||
if (root.val == 7) {
|
||||
// 记录解
|
||||
res.Add(new List<TreeNode>(path));
|
||||
return;
|
||||
}
|
||||
preOrder(root.left);
|
||||
preOrder(root.right);
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
# 回退
|
||||
|
|
|
@ -21,6 +21,7 @@ fn pre_order(res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>, path: &mut Vec<Rc<RefCel
|
|||
if node.borrow().val == 7 {
|
||||
// 记录解
|
||||
res.push(path.clone());
|
||||
return;
|
||||
}
|
||||
pre_order(res, path, node.borrow().left.clone());
|
||||
pre_order(res, path, node.borrow().right.clone());
|
||||
|
|
|
@ -20,6 +20,7 @@ func preOrder(root: TreeNode?) {
|
|||
if root.val == 7 {
|
||||
// 记录解
|
||||
res.append(path)
|
||||
return
|
||||
}
|
||||
preOrder(root: root.left)
|
||||
preOrder(root: root.right)
|
||||
|
|
|
@ -23,6 +23,7 @@ function preOrder(
|
|||
if (root.val === 7) {
|
||||
// 记录解
|
||||
res.push([...path]);
|
||||
return;
|
||||
}
|
||||
preOrder(root.left, path, res);
|
||||
preOrder(root.right, path, res);
|
||||
|
|
|
@ -199,9 +199,12 @@
|
|||
|
||||
!!! question "例题三"
|
||||
|
||||
在二叉树中搜索所有值为 $7$ 的节点,返回根节点到这些节点的路径,**路径中不能包含值为 $3$ 的节点**。
|
||||
在二叉树中搜索所有值为 $7$ 的节点,返回根节点到这些节点的路径,**路径中有且只有一个值为 $7$ 的节点,并且不能包含值为 $3$ 的节点**。
|
||||
|
||||
**解题思路**:在例题二的基础上添加剪枝操作,当遇到值为 $3$ 的节点时,则终止继续搜索。
|
||||
**解题思路**:在例题二的基础上添加剪枝操作。
|
||||
|
||||
- 当遇到值为 $7$ 的节点时,记录解并停止搜索。
|
||||
- 当遇到值为 $3$ 的节点时,则终止继续搜索。
|
||||
|
||||
=== "Java"
|
||||
|
||||
|
|
Loading…
Reference in a new issue