2.4学习总结

news/2025/2/5 5:40:53/

洛谷1305代码

#include<stdio.h>
#include<stdlib.h>
struct treenode {char val;struct treenode* left;struct treenode* right;
};
struct treenode* createnode(char val) {struct treenode* node = (struct treenode*)malloc(sizeof(struct treenode));node->val = val;node->left = NULL;node->right = NULL;return node;
}
struct treenode* buildtree(int n, char nodes[][4]) {struct treenode* root = NULL;struct treenode* nodesarray[26] = { NULL };for (int i = 0;i < n;i++) {char val = nodes[i][0];char left = nodes[i][1];char right = nodes[i][2];if (nodesarray[val - 'a'] == NULL) {nodesarray[val - 'a'] = createnode(val);}if (left != '*') {if (nodesarray[left - 'a'] == NULL) {nodesarray[left - 'a'] = createnode(left);}nodesarray[val - 'a']->right = nodesarray[right - 'a'];}if (i == 0) {root = nodesarray[val - 'a'];}}return root;
}
void preorderTraversal(struct treenode* root) {if (root == NULL) {return;}printf("%c", root->val);preorderTraversal(root->left);preorderTraversal(root->right);
}
int main() {int n;scanf("%d", & n);char nodes[n][4];for (int i = 0;i < n;i++) {scanf("%s", nodes[i]);}struct treenode* root = buildtree(n, nodes);preorderTraversal(root);printf("\n");return 0;
}

前序遍历方法(dfs)二叉树

struct MyStruct
{char l, r;
}tree[200];
void dfs(char pos)
{cout << pos;if (tree[pos].l != '*') dfs(tree[pos].l);if (tree[pos].r != '*') dfs(tree[pos].r);
}

前序遍历

void preorderTraversal(struct treenode* root) {if (root == NULL) {return;}printf("%c", root->val);preorderTraversal(root->left);preorderTraversal(root->right);
}

洛谷1160代码

#include <stdio.h>
#include <stdlib.h>
typedef struct Node {int id;struct Node* prev;struct Node* next;
} Node;
Node* createNode(int id) {Node* newNode = (Node*)malloc(sizeof(Node));newNode->id = id;newNode->prev = NULL;newNode->next = NULL;return newNode;
}
//左插
void insertLeft(Node* target, Node* newNode) {newNode->next = target;newNode->prev = target->prev;if (target->prev != NULL) {target->prev->next = newNode;}target->prev = newNode;
}
//右插
void insertRight(Node* target, Node* newNode) {newNode->prev = target;newNode->next = target->next;if (target->next != NULL) {target->next->prev = newNode;}target->next = newNode;
}
//去掉
void removeNode(Node* node) {if (node->prev != NULL) {node->prev->next = node->next;}if (node->next != NULL) {node->next->prev = node->prev;}free(node);
}
int main() {int N, M;scanf("%d", &N);Node* head = createNode(1);Node* nodes[N+1];nodes[1] = head;for (int i = 2; i <= N; i++) {int k, p;scanf("%d %d", &k, &p);Node* newNode = createNode(i);nodes[i] = newNode;if (p == 0) {insertLeft(nodes[k], newNode);} else {insertRight(nodes[k], newNode);}}scanf("%d", &M);for (int i = 0; i < M; i++) {int x;scanf("%d", &x);if (nodes[x] != NULL) {removeNode(nodes[x]);nodes[x] = NULL;}}Node* current = head;while (current->prev != NULL) {current = current->prev;}while (current != NULL) {printf("%d ", current->id);current = current->next;}return 0;
}

 

 


http://www.ppmy.cn/news/1569424.html

相关文章

Python 科学计算

&#x1f9d1; 博主简介&#xff1a;CSDN博客专家&#xff0c;历代文学网&#xff08;PC端可以访问&#xff1a;https://literature.sinhy.com/#/literature?__c1000&#xff0c;移动端可微信小程序搜索“历代文学”&#xff09;总架构师&#xff0c;15年工作经验&#xff0c;…

GESP2023年12月认证C++六级( 第三部分编程题(1)闯关游戏)

参考程序代码&#xff1a; #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <string> #include <map> #include <iostream> #include <cmath> using namespace std;const int N 10…

新月军事战略分析系统使用手册

新月人物传记&#xff1a; 人物传记之新月篇-CSDN博客 相关故事链接&#xff1a;星际智慧农业系统&#xff08;SAS&#xff09;&#xff0c;智慧农业的未来篇章-CSDN博客 “新月智能武器系统”CIWS&#xff0c;开启智能武器的新纪元-CSDN博客 “新月之智”智能战术头盔系统&…

[EAI-023] FAST,机器人动作专用的Tokenizer,提高VLA模型的能力和训练效率

Paper Card 论文标题&#xff1a;FAST: Efficient Action Tokenization for Vision-Language-Action Models 论文作者&#xff1a;Karl Pertsch, Kyle Stachowicz, Brian Ichter, Danny Driess, Suraj Nair, Quan Vuong, Oier Mees, Chelsea Finn, Sergey Levine 论文链接&…

Day07:缓存-数据淘汰策略

Redis的数据淘汰策略有哪些 ? &#xff08;key过期导致的&#xff09; 在redis中提供了两种数据过期删除策略 第一种是惰性删除&#xff0c;在设置该key过期时间后&#xff0c;我们不去管它&#xff0c;当需要该key时&#xff0c;我们再检查其是否过期&#xff0c;如果过期&…

PVE 中 Debian 虚拟机崩溃后,硬盘数据怎么恢复

问题 在 PVE 中给 Debian 虚拟机新分配硬盘后&#xff0c;通过 Debian 虚拟机开启 Samba 共享该硬盘。如果这个 Debian 虚拟机崩溃后&#xff0c;怎么恢复 Samba 共享硬盘数据。 方法 开启 Samba 共享相关知识&#xff1a;挂载硬盘和开启Samba共享。 新建一个虚拟机&#xf…

【游戏设计原理】97 - 空间感知

一、游戏空间的类型 将游戏设计中的空间设计单独提取出来&#xff0c;可以根据其结构、功能和玩家的交互方式划分为以下几种主要类型。这些类型可以单独存在&#xff0c;也可以组合使用&#xff0c;以创造更加复杂和有趣的游戏体验。 1. 线性空间 定义&#xff1a;空间设计是…

GPT与Deepseek等数据驱动AI的缺点

当前数据驱动的AI&#xff08;包括GPT与Deepseek等各种大小模型&#xff09;只进行了数/物理性的初步探索&#xff0c;尚未触及人机环境生态系统的复杂性。也就是说&#xff0c;当前的数据驱动型 AI&#xff0c;虽然在处理大量数据、解决特定任务方面取得了显著进展&#xff0c…