fix preorder_traversal_iii_compact code

This commit is contained in:
krahets
2023-09-09 00:51:00 +08:00
parent 2217ffc447
commit f71b2a40da
14 changed files with 17 additions and 37 deletions

View File

@@ -35,7 +35,7 @@ void undoChoice(vector *state, TreeNode *choice) {
vectorPopback(state);
}
/* 前序遍历:例题三 */
/* 回溯算法:例题三 */
void backtrack(vector *state, vector *choices, vector *res) {
// 检查是否为解
if (isSolution(state)) {

View File

@@ -30,16 +30,16 @@ struct hashMapChaining {
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;
HashMapChaining *hashmap = (HashMapChaining *)malloc(sizeof(HashMapChaining));
hashMapChaining *hashmap = (hashMapChaining *)malloc(sizeof(hashMapChaining));
// 初始化数组
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++) {
Pair *pair = &hashmap->buckets[i];
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;
}
/* 负载因子 */
double loadFactor(HashMapChaining *hashmap) {
double loadFactor(hashMapChaining *hashmap) {
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);
Pair *pair = &hashmap->buckets[index];
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) {
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);
Pair *pair = &hashmap->buckets[index];
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;
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++) {
printf("[");
Pair *pair = &hashmap->buckets[i];
@@ -214,7 +214,7 @@ void print(HashMapChaining *hashmap) {
/* Driver Code */
int main() {
/* 初始化哈希表 */
HashMapChaining *map = newHashMapChaining();
hashMapChaining *map = newHashMapChaining();
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)

View File

@@ -20,8 +20,6 @@ void preOrder(TreeNode *root) {
if (root->val == 7) {
// 记录解
res.push_back(path);
path.pop_back();
return;
}
preOrder(root->left);
preOrder(root->right);

View File

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

View File

@@ -22,8 +22,6 @@ void preOrder(
if (root.val == 7) {
// 记录解
res.add(List.from(path));
path.removeLast();
return;
}
preOrder(root.left, path, res);
preOrder(root.right, path, res);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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