mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	fix preorder_traversal_iii_compact code
This commit is contained in:
		@ -35,7 +35,7 @@ void undoChoice(vector *state, TreeNode *choice) {
 | 
				
			|||||||
    vectorPopback(state);
 | 
					    vectorPopback(state);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 前序遍历:例题三 */
 | 
					/* 回溯算法:例题三 */
 | 
				
			||||||
void backtrack(vector *state, vector *choices, vector *res) {
 | 
					void backtrack(vector *state, vector *choices, vector *res) {
 | 
				
			||||||
    // 检查是否为解
 | 
					    // 检查是否为解
 | 
				
			||||||
    if (isSolution(state)) {
 | 
					    if (isSolution(state)) {
 | 
				
			||||||
 | 
				
			|||||||
@ -30,16 +30,16 @@ struct hashMapChaining {
 | 
				
			|||||||
    Pair *buckets;    // 桶数组
 | 
					    Pair *buckets;    // 桶数组
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct hashMapChaining HashMapChaining;
 | 
					typedef struct hashMapChaining hashMapChaining;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 函数声明
 | 
					// 函数声明
 | 
				
			||||||
void extend(HashMapChaining *hashmap);
 | 
					void extend(hashMapChaining *hashmap);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 初始化桶数组 */
 | 
					/* 初始化桶数组 */
 | 
				
			||||||
HashMapChaining *newHashMapChaining() {
 | 
					hashMapChaining *newHashMapChaining() {
 | 
				
			||||||
    // 为哈希表分配空间
 | 
					    // 为哈希表分配空间
 | 
				
			||||||
    int tableSize = 4;
 | 
					    int tableSize = 4;
 | 
				
			||||||
    HashMapChaining *hashmap = (HashMapChaining *)malloc(sizeof(HashMapChaining));
 | 
					    hashMapChaining *hashmap = (hashMapChaining *)malloc(sizeof(hashMapChaining));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // 初始化数组
 | 
					    // 初始化数组
 | 
				
			||||||
    hashmap->buckets = (Pair *)malloc(sizeof(Pair) * tableSize);
 | 
					    hashmap->buckets = (Pair *)malloc(sizeof(Pair) * tableSize);
 | 
				
			||||||
@ -54,7 +54,7 @@ HashMapChaining *newHashMapChaining() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 销毁哈希表 */
 | 
					/* 销毁哈希表 */
 | 
				
			||||||
void delHashMapChaining(HashMapChaining *hashmap) {
 | 
					void delHashMapChaining(hashMapChaining *hashmap) {
 | 
				
			||||||
    for (int i = 0; i < hashmap->capacity; i++) {
 | 
					    for (int i = 0; i < hashmap->capacity; i++) {
 | 
				
			||||||
        Pair *pair = &hashmap->buckets[i];
 | 
					        Pair *pair = &hashmap->buckets[i];
 | 
				
			||||||
        Node *node = pair->node;
 | 
					        Node *node = pair->node;
 | 
				
			||||||
@ -70,17 +70,17 @@ void delHashMapChaining(HashMapChaining *hashmap) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 哈希函数 */
 | 
					/* 哈希函数 */
 | 
				
			||||||
int hashFunc(HashMapChaining *hashmap, const int key) {
 | 
					int hashFunc(hashMapChaining *hashmap, const int key) {
 | 
				
			||||||
    return key % hashmap->capacity;
 | 
					    return key % hashmap->capacity;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 负载因子 */
 | 
					/* 负载因子 */
 | 
				
			||||||
double loadFactor(HashMapChaining *hashmap) {
 | 
					double loadFactor(hashMapChaining *hashmap) {
 | 
				
			||||||
    return (double)hashmap->size / (double)hashmap->capacity;
 | 
					    return (double)hashmap->size / (double)hashmap->capacity;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 查询操作 */
 | 
					/* 查询操作 */
 | 
				
			||||||
const char *get(HashMapChaining *hashmap, const int key) {
 | 
					const char *get(hashMapChaining *hashmap, const int key) {
 | 
				
			||||||
    int index = hashFunc(hashmap, key);
 | 
					    int index = hashFunc(hashmap, key);
 | 
				
			||||||
    Pair *pair = &hashmap->buckets[index];
 | 
					    Pair *pair = &hashmap->buckets[index];
 | 
				
			||||||
    Node *node = pair->node;
 | 
					    Node *node = pair->node;
 | 
				
			||||||
@ -93,7 +93,7 @@ const char *get(HashMapChaining *hashmap, const int key) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 添加操作 */
 | 
					/* 添加操作 */
 | 
				
			||||||
void put(HashMapChaining *hashmap, const int key, char *val) {
 | 
					void put(hashMapChaining *hashmap, const int key, char *val) {
 | 
				
			||||||
    if (loadFactor(hashmap) > hashmap->loadThres) {
 | 
					    if (loadFactor(hashmap) > hashmap->loadThres) {
 | 
				
			||||||
        extend(hashmap);
 | 
					        extend(hashmap);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -134,7 +134,7 @@ void put(HashMapChaining *hashmap, const int key, char *val) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 删除操作 */
 | 
					/* 删除操作 */
 | 
				
			||||||
void removeItem(HashMapChaining *hashmap, int key) {
 | 
					void removeItem(hashMapChaining *hashmap, int key) {
 | 
				
			||||||
    int index = hashFunc(hashmap, key);
 | 
					    int index = hashFunc(hashmap, key);
 | 
				
			||||||
    Pair *pair = &hashmap->buckets[index];
 | 
					    Pair *pair = &hashmap->buckets[index];
 | 
				
			||||||
    Node *node = pair->node;
 | 
					    Node *node = pair->node;
 | 
				
			||||||
@ -161,7 +161,7 @@ void removeItem(HashMapChaining *hashmap, int key) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 扩容哈希表 */
 | 
					/* 扩容哈希表 */
 | 
				
			||||||
void extend(HashMapChaining *hashmap) {
 | 
					void extend(hashMapChaining *hashmap) {
 | 
				
			||||||
    // 暂存原哈希表
 | 
					    // 暂存原哈希表
 | 
				
			||||||
    Pair *oldBuckets = hashmap->buckets;
 | 
					    Pair *oldBuckets = hashmap->buckets;
 | 
				
			||||||
    int oldCapacity = hashmap->capacity;
 | 
					    int oldCapacity = hashmap->capacity;
 | 
				
			||||||
@ -195,7 +195,7 @@ void extend(HashMapChaining *hashmap) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 打印哈希表 */
 | 
					/* 打印哈希表 */
 | 
				
			||||||
void print(HashMapChaining *hashmap) {
 | 
					void print(hashMapChaining *hashmap) {
 | 
				
			||||||
    for (int i = 0; i < hashmap->capacity; i++) {
 | 
					    for (int i = 0; i < hashmap->capacity; i++) {
 | 
				
			||||||
        printf("[");
 | 
					        printf("[");
 | 
				
			||||||
        Pair *pair = &hashmap->buckets[i];
 | 
					        Pair *pair = &hashmap->buckets[i];
 | 
				
			||||||
@ -214,7 +214,7 @@ void print(HashMapChaining *hashmap) {
 | 
				
			|||||||
/* Driver Code */
 | 
					/* Driver Code */
 | 
				
			||||||
int main() {
 | 
					int main() {
 | 
				
			||||||
    /* 初始化哈希表 */
 | 
					    /* 初始化哈希表 */
 | 
				
			||||||
    HashMapChaining *map = newHashMapChaining();
 | 
					    hashMapChaining *map = newHashMapChaining();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 添加操作 */
 | 
					    /* 添加操作 */
 | 
				
			||||||
    // 在哈希表中添加键值对 (key, value)
 | 
					    // 在哈希表中添加键值对 (key, value)
 | 
				
			||||||
 | 
				
			|||||||
@ -20,8 +20,6 @@ void preOrder(TreeNode *root) {
 | 
				
			|||||||
    if (root->val == 7) {
 | 
					    if (root->val == 7) {
 | 
				
			||||||
        // 记录解
 | 
					        // 记录解
 | 
				
			||||||
        res.push_back(path);
 | 
					        res.push_back(path);
 | 
				
			||||||
        path.pop_back();
 | 
					 | 
				
			||||||
        return;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    preOrder(root->left);
 | 
					    preOrder(root->left);
 | 
				
			||||||
    preOrder(root->right);
 | 
					    preOrder(root->right);
 | 
				
			||||||
 | 
				
			|||||||
@ -21,8 +21,6 @@ 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));
 | 
				
			||||||
            path.RemoveAt(path.Count - 1);
 | 
					 | 
				
			||||||
            return;
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        preOrder(root.left);
 | 
					        preOrder(root.left);
 | 
				
			||||||
        preOrder(root.right);
 | 
					        preOrder(root.right);
 | 
				
			||||||
 | 
				
			|||||||
@ -22,8 +22,6 @@ void preOrder(
 | 
				
			|||||||
  if (root.val == 7) {
 | 
					  if (root.val == 7) {
 | 
				
			||||||
    // 记录解
 | 
					    // 记录解
 | 
				
			||||||
    res.add(List.from(path));
 | 
					    res.add(List.from(path));
 | 
				
			||||||
    path.removeLast();
 | 
					 | 
				
			||||||
    return;
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  preOrder(root.left, path, res);
 | 
					  preOrder(root.left, path, res);
 | 
				
			||||||
  preOrder(root.right, path, res);
 | 
					  preOrder(root.right, path, res);
 | 
				
			||||||
 | 
				
			|||||||
@ -19,8 +19,6 @@ func preOrderIII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {
 | 
				
			|||||||
	if root.Val.(int) == 7 {
 | 
						if root.Val.(int) == 7 {
 | 
				
			||||||
		// 记录解
 | 
							// 记录解
 | 
				
			||||||
		*res = append(*res, *path)
 | 
							*res = append(*res, *path)
 | 
				
			||||||
		*path = (*path)[:len(*path)-1]
 | 
					 | 
				
			||||||
		return
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	preOrderIII(root.Left, res, path)
 | 
						preOrderIII(root.Left, res, path)
 | 
				
			||||||
	preOrderIII(root.Right, res, path)
 | 
						preOrderIII(root.Right, res, path)
 | 
				
			||||||
 | 
				
			|||||||
@ -24,8 +24,6 @@ public class preorder_traversal_iii_compact {
 | 
				
			|||||||
        if (root.val == 7) {
 | 
					        if (root.val == 7) {
 | 
				
			||||||
            // 记录解
 | 
					            // 记录解
 | 
				
			||||||
            res.add(new ArrayList<>(path));
 | 
					            res.add(new ArrayList<>(path));
 | 
				
			||||||
            path.remove(path.size() - 1);
 | 
					 | 
				
			||||||
            return;
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        preOrder(root.left);
 | 
					        preOrder(root.left);
 | 
				
			||||||
        preOrder(root.right);
 | 
					        preOrder(root.right);
 | 
				
			||||||
 | 
				
			|||||||
@ -18,8 +18,6 @@ function preOrder(root, path, res) {
 | 
				
			|||||||
    if (root.val === 7) {
 | 
					    if (root.val === 7) {
 | 
				
			||||||
        // 记录解
 | 
					        // 记录解
 | 
				
			||||||
        res.push([...path]);
 | 
					        res.push([...path]);
 | 
				
			||||||
        path.pop();
 | 
					 | 
				
			||||||
        return;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    preOrder(root.left, path, res);
 | 
					    preOrder(root.left, path, res);
 | 
				
			||||||
    preOrder(root.right, path, res);
 | 
					    preOrder(root.right, path, res);
 | 
				
			||||||
 | 
				
			|||||||
@ -20,8 +20,6 @@ def pre_order(root: TreeNode):
 | 
				
			|||||||
    if root.val == 7:
 | 
					    if root.val == 7:
 | 
				
			||||||
        # 记录解
 | 
					        # 记录解
 | 
				
			||||||
        res.append(list(path))
 | 
					        res.append(list(path))
 | 
				
			||||||
        path.pop()
 | 
					 | 
				
			||||||
        return
 | 
					 | 
				
			||||||
    pre_order(root.left)
 | 
					    pre_order(root.left)
 | 
				
			||||||
    pre_order(root.right)
 | 
					    pre_order(root.right)
 | 
				
			||||||
    # 回退
 | 
					    # 回退
 | 
				
			||||||
 | 
				
			|||||||
@ -21,8 +21,6 @@ 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());
 | 
				
			||||||
            path.remove(path.len() -  1);
 | 
					 | 
				
			||||||
            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());
 | 
				
			||||||
 | 
				
			|||||||
@ -20,8 +20,6 @@ func preOrder(root: TreeNode?) {
 | 
				
			|||||||
    if root.val == 7 {
 | 
					    if root.val == 7 {
 | 
				
			||||||
        // 记录解
 | 
					        // 记录解
 | 
				
			||||||
        res.append(path)
 | 
					        res.append(path)
 | 
				
			||||||
        path.removeLast()
 | 
					 | 
				
			||||||
        return
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    preOrder(root: root.left)
 | 
					    preOrder(root: root.left)
 | 
				
			||||||
    preOrder(root: root.right)
 | 
					    preOrder(root: root.right)
 | 
				
			||||||
 | 
				
			|||||||
@ -23,8 +23,6 @@ function preOrder(
 | 
				
			|||||||
    if (root.val === 7) {
 | 
					    if (root.val === 7) {
 | 
				
			||||||
        // 记录解
 | 
					        // 记录解
 | 
				
			||||||
        res.push([...path]);
 | 
					        res.push([...path]);
 | 
				
			||||||
        path.pop();
 | 
					 | 
				
			||||||
        return;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    preOrder(root.left, path, res);
 | 
					    preOrder(root.left, path, res);
 | 
				
			||||||
    preOrder(root.right, path, res);
 | 
					    preOrder(root.right, path, res);
 | 
				
			||||||
 | 
				
			|||||||
@ -8,6 +8,6 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
!!! abstract
 | 
					!!! abstract
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    数据结构的世界如同一睹厚实的砖墙。
 | 
					    数据结构的世界如同一堵厚实的砖墙。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    数组的砖块整齐排列,逐个紧贴。链表的砖块分散各处,连接的藤蔓自由地穿梭于砖缝之间。
 | 
					    数组的砖块整齐排列,逐个紧贴。链表的砖块分散各处,连接的藤蔓自由地穿梭于砖缝之间。
 | 
				
			||||||
 | 
				
			|||||||
@ -761,7 +761,7 @@
 | 
				
			|||||||
    [class]{}-[func]{backtrack}
 | 
					    [class]{}-[func]{backtrack}
 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
根据题意,我们在找到值为 7 的节点后应该继续搜索,**因此需要将记录解之后的 `return` 语句删除**。下图对比了保留或删除 `return` 语句的搜索过程。
 | 
					根据题意,我们在找到值为 $7$ 的节点后应该继续搜索,**因此需要将记录解之后的 `return` 语句删除**。下图对比了保留或删除 `return` 语句的搜索过程。
 | 
				
			||||||
 | 
					
 | 
				
			||||||

 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -776,7 +776,7 @@
 | 
				
			|||||||
| 名词                | 定义                                                                       | 例题三                                                               |
 | 
					| 名词                | 定义                                                                       | 例题三                                                               |
 | 
				
			||||||
| ------------------- | -------------------------------------------------------------------------- | -------------------------------------------------------------------- |
 | 
					| ------------------- | -------------------------------------------------------------------------- | -------------------------------------------------------------------- |
 | 
				
			||||||
| 解 Solution         | 解是满足问题特定条件的答案,可能有一个或多个                               | 根节点到节点 $7$ 的满足约束条件的所有路径                            |
 | 
					| 解 Solution         | 解是满足问题特定条件的答案,可能有一个或多个                               | 根节点到节点 $7$ 的满足约束条件的所有路径                            |
 | 
				
			||||||
| 约束条件 Constraint | 约束条件是问题中限制解的可行性的条件,通常用于剪枝                         | 路径中不包含节点 $3$ ,只包含一个节点 $7$                            |
 | 
					| 约束条件 Constraint | 约束条件是问题中限制解的可行性的条件,通常用于剪枝                         | 路径中不包含节点 $3$                                                 |
 | 
				
			||||||
| 状态 State          | 状态表示问题在某一时刻的情况,包括已经做出的选择                           | 当前已访问的节点路径,即 `path` 节点列表                             |
 | 
					| 状态 State          | 状态表示问题在某一时刻的情况,包括已经做出的选择                           | 当前已访问的节点路径,即 `path` 节点列表                             |
 | 
				
			||||||
| 尝试 Attempt        | 尝试是根据可用选择来探索解空间的过程,包括做出选择,更新状态,检查是否为解 | 递归访问左(右)子节点,将节点添加进 `path` ,判断节点的值是否为 $7$ |
 | 
					| 尝试 Attempt        | 尝试是根据可用选择来探索解空间的过程,包括做出选择,更新状态,检查是否为解 | 递归访问左(右)子节点,将节点添加进 `path` ,判断节点的值是否为 $7$ |
 | 
				
			||||||
| 回退 Backtracking   | 回退指遇到不满足约束条件的状态时,撤销前面做出的选择,回到上一个状态       | 当越过叶结点、结束结点访问、遇到值为 $3$ 的节点时终止搜索,函数返回  |
 | 
					| 回退 Backtracking   | 回退指遇到不满足约束条件的状态时,撤销前面做出的选择,回到上一个状态       | 当越过叶结点、结束结点访问、遇到值为 $3$ 的节点时终止搜索,函数返回  |
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user