From b84c3ba78354def6002ec52905a42c247ba3f676 Mon Sep 17 00:00:00 2001 From: e2hang <2099307493@qq.com> Date: Sat, 8 Nov 2025 13:46:52 +0800 Subject: [PATCH] Homework New --- Exercise/Homework2/readme.md | 103 ++++++++++++++++++++ Exercise/Homework3/readmd.md | 120 +++++++++++++++++++++++ Exercise/Homework4/Q1.cpp | 97 +++++++++++++++++++ Exercise/Homework4/Q2.cpp | 121 +++++++++++++++++++++++ Exercise/Homework4/Q3.cpp | 181 +++++++++++++++++++++++++++++++++++ Exercise/Homework4/readme.md | 93 ++++++++++++++++++ Exercise/Homework5/Q1.cpp | 151 +++++++++++++++++++++++++++++ Exercise/Homework5/readme.md | 135 ++++++++++++++++++++++++++ 8 files changed, 1001 insertions(+) create mode 100644 Exercise/Homework2/readme.md create mode 100644 Exercise/Homework3/readmd.md create mode 100644 Exercise/Homework4/Q1.cpp create mode 100644 Exercise/Homework4/Q2.cpp create mode 100644 Exercise/Homework4/Q3.cpp create mode 100644 Exercise/Homework4/readme.md create mode 100644 Exercise/Homework5/Q1.cpp create mode 100644 Exercise/Homework5/readme.md diff --git a/Exercise/Homework2/readme.md b/Exercise/Homework2/readme.md new file mode 100644 index 0000000..5ff1b42 --- /dev/null +++ b/Exercise/Homework2/readme.md @@ -0,0 +1,103 @@ +Q1 +``` +编写程序检查给定字符串中包含的括号是否正确匹配,本题中的括号有{ }、[ ]、( )、< >四种。另外再加上一个新的约束条件:当有多种括号嵌套时,嵌套的顺序应为{ → [ → ( → <,即a–g+b∗[(d∗)]、a+[b+(c–d)∗e]都是正确的匹配,而a+(b∗[c+d])则不是正确匹配。注意本题不允许相同类型括号的嵌套,即a+(b∗(c+d))不是正确匹配。本题不需要判断表达式是否合法,只需判断字符串中包含的括号是否正确匹配。 + +输入格式: +第一行为一个整数n,表示字符串的个数。接下来n行,每行为一个字符串。1)d]e}x +[()] +((())) +<>()[]{} +[{}] +x=y+{z+(b)} +][() +输出样例1: +Fail +Match +Match +Fail +Match +Fail +Match +Fail +输入样例2: +6 +{[afds(a)]}yt +[()rew] +<>()[wre]{} +[{qw}] +rew{(weq)}jjk +<><{}>[][](){[{}]} +输出样例2: +Match +Match +Match +Fail +Match +Fail +代码长度限制 +16 KB +时间限制 +50 ms +内存限制 +30 MB +栈限制 +8192 KB +``` +Q2 +``` +从A点到B点有n个格子,小明现在要从A点到B点,小明吃了些东西,补充了一下体力,他可以一步迈一个格子,也可以一步迈两个格子,也可以一步迈3个格子,也可以一步迈4个格子。请编写程序计算小明从A点到B点一共有多少种走法。 + +grid2.jpg + +输入格式: +输入包含多组数据,第一行为一个整数m,m不超过10000,表示输入数据组数。接下来m行,每行为一个整数n(n不超过100,且保证对应的结果小于2 +31 + ),表示从A点到B点的格子数。 + +输出格式: +输出为m个整数,表示对于每组数据小明从A点到B点的走法数。 + +输入样例: +2 +5 +3 +输出样例: +15 +4 +``` + +Q3 +``` +本学期的《数据结构》课上,老师曾结合汉诺塔问题,介绍了使用栈消除递归的方法,从而将汉诺塔问题的递归算法转换为非递归算法,本题请你编程实现上述非递归算法,即使用栈以非递归形式求解汉诺塔问题。将n个圆盘自A柱移至C柱(可途经B柱),并输出求解过中栈的最大容量(最多存储了多少个四元组)。 + +备注:本题将在机器评测后进行人工核验,若有同学未按题目要求,仅使用递归程序通过本题,本题记为0分。 +image.png + +输入格式: +输入为一个整数n,表示初始时A柱上的圆盘数目,n不超过20。 + +输出格式: +按顺序输出各动作,每个动作占一行,格式为“Move disk from x to y”,表示将x柱顶端的圆盘移至y柱。 + +最后一行为一个整数,表示求解过程中栈的最大容量。 + +输入样例: +3 +输出样例: +Move disk from A to C +Move disk from A to B +Move disk from C to B +Move disk from A to C +Move disk from B to A +Move disk from B to C +Move disk from A to C +5 +``` \ No newline at end of file diff --git a/Exercise/Homework3/readmd.md b/Exercise/Homework3/readmd.md new file mode 100644 index 0000000..c1742ab --- /dev/null +++ b/Exercise/Homework3/readmd.md @@ -0,0 +1,120 @@ +Q1 +``` +给定一个中缀表达式,请编写程序计算该表达式的值。表达式包含+、-、*、/、^、(、),所有运算均为二元运算,操作数均为正整数,但可能不止一位,不超过10位。运算结果为整数,值域为[−2 +31 + ,2 +31 + )。除法运算结果若为小数则进行截尾取整。若除法运算中除数为0,则输出INVALID。幂运算须自行实现,不允许调用pow等系统函数。 + +测试数据保证幂运算中指数为非负,底数不为0,且不会出现诸如n^a^b连续多次幂运算。 + +输入格式: +输入为多行,每行为一个长度不超过1000的字符串,表示中缀表达式。 + +输出格式: +对每个表达式输出一行:为一个整数(表达式的值)或为一个字符串INVALID。 + +输入样例: +5+(10*2)-6 +8*(999+1) +1+5/(1-1) +7*2^3 + +输出样例: +19 +8000 +INVALID +56 +代码长度限制 +16 KB +时间限制 +50 ms +内存限制 +64 MB +栈限制 +8192 KB +``` + +Q2 +``` +2021年11月6日,英雄联盟全球总决赛打响,中国电子竞技战队Edward Gaming(EDG)以3:2力克韩国强敌DWG KIA(DK)战队,历史上首次夺得全球总冠军。一时间全网沸腾,大家纷纷在社交平台上直呼“edgnb”。现给定一段文本,请编写程序识别出连续的k个“edgnb”组成的字符串在该文本中出现了多少次。 + +输入格式: +第一行为1个整数T,表示数据组数。对于每组数据,第一行为1个字符串,表示给定的文本。第二行为1个整数k,含义如题目所述。(1≤T≤10。各组数据给定的字符串长度之和不超过10 +5 + ,且字符串中只包含a-z的小写字母。k≥1且k×5小于给定字符串长度)。 + +输出格式: +对于每组数据输出一行,为1个整数,表示所求的出现次数。 + +输入样例: +5 +xyzedgnbabcedgnb +1 +xyzedgnbabcedgnb +2 +defedgnbedgnbxyz +2 +edgnbedgnbedgnb +2 +fxedgnbedgnbedgnbedgnbmem +3 + +输出样例: +2 +0 +1 +2 +2 + +数据规模: +测试点0:5≤T≤10,400≤T个字符串长度之和≤500,k=1 +测试点1:5≤T≤10,400≤T个字符串长度之和≤500,k≥1 +测试点2:5≤T≤10,4000≤T个字符串长度之和≤5000,k≥1 +测试点3:1≤T≤3,90000≤T个字符串长度之和≤100000,k≥1 +测试点4:1≤T≤3,90000≤T个字符串长度之和≤100000,k≥1 + +代码长度限制 +16 KB +时间限制 +400 ms +内存限制 +64 MB +栈限制 +8192 KB +``` + +Q3 +``` +波比和哈丽在玩一个字母游戏,波比给出一个字符串S,要求哈丽按照一定规则,基于该字符串算出一个数字X。 + +规则是: + +(1)求出S的最长重复后缀P(P是S的后缀且在S中出现大于1次,例如yacbacba的最长重复后缀是acba), + +(2)求出在S中去除第二长相等前后缀(S中所有相等的前后缀中第2长者,例如abcabcxxxabcabc中最长相等前后缀是abcabc,第二长的相等前后缀则是abc)后剩下的子串Q(例如abcabcxxxabcabc去除第二长相等前后缀后,剩下abcxxxabc)。 + +则X=P的长度+Q的长度。 + +注意一个字符串不能称为自己的前缀或后缀。子串Q至少为空串,其长度大于等于0,不能为负数。 + +请编写程序帮助哈丽根据给定字符串S,根据上述规则计算出数字X。 + +输入格式: +输入为若干行,每行为一个字符串,包含不超过100000个字母。 + +输出格式: +输出为若干行,每行一个整数,表示输入字符串所计算出的数字。 + +输入样例: +abcabcxxxabcabc +xacbacba +abc +aaa + +输出样例: +15 +12 +3 +3 +``` \ No newline at end of file diff --git a/Exercise/Homework4/Q1.cpp b/Exercise/Homework4/Q1.cpp new file mode 100644 index 0000000..b655713 --- /dev/null +++ b/Exercise/Homework4/Q1.cpp @@ -0,0 +1,97 @@ +#include +#include +using namespace std; + +template +struct Node { + T element; + Node* left; + Node* right; + + Node(const T& e) : element(e), left(nullptr), right(nullptr) {} +}; + +template +class BinTree { +public: + Node* root; + int height; + vector arr; + + BinTree() : root(nullptr), height(0) {} + BinTree(const vector& arrs) : arr(arrs), height(0) {} + void test() { + for (auto x : arr) { + cout << x << " "; + } + } + Node* returnroot() { + return this->root; + } + + Node* build(int& index) { + if (index >= arr.size() || arr[index] == 0) { + index++; + return nullptr; + } + + Node* node = new Node(arr[index]); + index++; + node->left = build(index); + //cout << "build left "; + node->right = build(index); + //cout << "build right "; + + return node; + } + + void buildTree() { + int index = 0; + this->root = build(index); + } + + // + void preorder(Node* node) { + if (node == nullptr) return; + + cout << node->element << " "; + preorder(node->left); + preorder(node->right); + } + void inorder(Node* node) { + if (node == nullptr) return; + + inorder(node->left); + cout << node->element << " "; + inorder(node->right); + } + void postorder(Node* node) { + if (node == nullptr) return; + + postorder(node->left); + postorder(node->right); + cout << node->element << " "; + } +}; + +int main() { + int x = 0; + vector arr; + int tmp; + Node* parent; + while (cin >> tmp) { + arr.push_back(tmp); + } + BinTree tree(arr); + tree.buildTree(); + //tree.test(); + auto r = tree.root; + tree.preorder(r); + cout << endl; + tree.inorder(r); + cout << endl; + tree.postorder(r); + cout << endl; + + return 0; +} diff --git a/Exercise/Homework4/Q2.cpp b/Exercise/Homework4/Q2.cpp new file mode 100644 index 0000000..5478aaf --- /dev/null +++ b/Exercise/Homework4/Q2.cpp @@ -0,0 +1,121 @@ +#include +#include +#include +#include +using namespace std; + +template +struct Node { + T element; + Node* left; + Node* right; + Node* parent; + + Node(const T& e, Node* p) : element(e), parent(p), left(nullptr), right(nullptr) {} +}; + +template +class BinTree { +public: + Node* root; + int height; + vector arr; + + BinTree() : root(nullptr), height(0) {} + BinTree(const vector& arrs) : arr(arrs), height(0) {} + void test() { + for (auto x : arr) { + cout << x << " "; + } + } + Node* returnroot() { + return this->root; + } + + Node* build(int& index, Node* parent) { + if (index >= arr.size() || arr[index] == 0) { + index++; + return nullptr; + } + + Node* node = new Node(arr[index], parent); + index++; + node->left = build(index, node); + node->right = build(index, node); + + return node; + } + + void buildTree() { + int index = 0; + this->root = build(index, nullptr); + } + + // + void preorder(Node* node) { + if (node == nullptr) return; + + cout << node->element << " "; + preorder(node->left); + preorder(node->right); + } + void inorder(Node* node) { + if (node == nullptr) return; + + inorder(node->left); + cout << node->element << ", parent"; + //cout << ( node->parent == nullptr ? 0 : node->parent->element) << endl; + inorder(node->right); + } + void postorder(Node* node) { + if (node == nullptr) return; + + postorder(node->left); + postorder(node->right); + cout << node->element << " "; + } + Node* inorder2(Node* node, const T& x) { + if (node == nullptr) return nullptr; + + if (node->element == x) { + return node; + } + + Node* leftResult = inorder2(node->left, x); + if (leftResult != nullptr) { + return leftResult; + } + + Node* rightResult = inorder2(node->right, x); + return rightResult; + } + void findparent(const T& e) { + auto x = inorder2(this->root, e); + if (x == nullptr || x->parent == nullptr) cout << 0 << endl; + else cout << x->parent->element << endl; + } +}; + +int main() { + int x = 0; + vector arr; + string line; + getline(cin, line); + stringstream ss(line); + int num; + + while (ss >> num) { + arr.push_back(num); + } + BinTree tree(arr); + tree.buildTree(); + + int m; + cin >> m; + for (int i = 0; i < m; i++) { + int t; + cin >> t; + tree.findparent(t); + } + return 0; +} diff --git a/Exercise/Homework4/Q3.cpp b/Exercise/Homework4/Q3.cpp new file mode 100644 index 0000000..78d2920 --- /dev/null +++ b/Exercise/Homework4/Q3.cpp @@ -0,0 +1,181 @@ +#include +#include +#include +using namespace std; + +template +struct Node { + T element; + Node* left; + Node* right; + Node* parent; + + Node(const T& e, Node* p) : element(e), parent(p), left(nullptr), right(nullptr) {} +}; + +template +class BinTree { +public: + Node* root; + int height; + vector arr; + + BinTree() : root(nullptr), height(0) {} + BinTree(const vector& arrs) : arr(arrs), height(0) {} + void test() { + for (auto x : arr) { + cout << x << " "; + } + } + Node* returnroot() { + return this->root; + } + + Node* build(int& index, Node* parent) { + if (index >= arr.size() || arr[index] == 0) { + index++; + return nullptr; + } + + Node* node = new Node(arr[index], parent); + index++; + node->left = build(index, node); + node->right = build(index, node); + + return node; + } + + void buildTree() { + int index = 0; + this->root = build(index, nullptr); + } + + Node* create(const string& post, const string& in, + int ps, int pe, int is, int ie, bool& ok) { + if (ps > pe || is > ie) { + return nullptr; + } + + T root = post[pe]; + int pos = -1; + for (int i = is; i <= ie; i++) { + if (in[i] == root) { + pos = i; + break; + } + } + + if (pos == -1) { + ok = false; + return nullptr; + } + + Node* node = new Node(root, nullptr); + int len = pos - is; + + node->left = create(post, in, ps, ps + len - 1, is, pos - 1, ok); + node->right = create(post, in, ps + len, pe - 1, pos + 1, ie, ok); + + return node; + } + + int getHeight(Node* node) { + if (node == nullptr) return 0; + int lh = getHeight(node->left); + int rh = getHeight(node->right); + if (lh > rh) return lh + 1; + else return rh + 1; + } + + string preorderStr(Node* node) { + if (node == nullptr) return ""; + string res = ""; + res += node->element; + res += preorderStr(node->left); + res += preorderStr(node->right); + return res; + } + + // + void preorder(Node* node) { + if (node == nullptr) return; + + cout << node->element << " "; + preorder(node->left); + preorder(node->right); + } + void inorder(Node* node) { + if (node == nullptr) return; + + inorder(node->left); + cout << node->element << ", parent"; + //cout << ( node->parent == nullptr ? 0 : node->parent->element) << endl; + inorder(node->right); + } + void postorder(Node* node) { + if (node == nullptr) return; + + postorder(node->left); + postorder(node->right); + cout << node->element << " "; + } + Node* inorder2(Node* node, const T& x) { + if (node == nullptr) return nullptr; + + if (node->element == x) { + return node; + } + + Node* leftResult = inorder2(node->left, x); + if (leftResult != nullptr) { + return leftResult; + } + + Node* rightResult = inorder2(node->right, x); + return rightResult; + } + void findparent(const T& e) { + auto x = inorder2(this->root, e); + if (x == nullptr || x->parent == nullptr) cout << 0 << endl; + else cout << x->parent->element << endl; + } +}; + +bool check(const string& post, const string& in) { + if (post.length() != in.length()) return false; + int cnt[26] = {0}; + for (int i = 0; i < post.length(); i++) { + cnt[post[i] - 'A']++; + } + for (int i = 0; i < in.length(); i++) { + cnt[in[i] - 'A']--; + if (cnt[in[i] - 'A'] < 0) return false; + } + return true; +} + +int main() { + string post, in; + while (getline(cin, post) && getline(cin, in)) { + if (post.empty() || in.empty()) break; + + if (!check(post, in)) { + cout << "INVALID" << endl; + continue; + } + + BinTree tree; + bool ok = true; + tree.root = tree.create(post, in, 0, post.length() - 1, 0, in.length() - 1, ok); + + if (!ok) { + cout << "INVALID" << endl; + } else { + tree.height = tree.getHeight(tree.root) - 1; + cout << tree.height << endl; + string pre = tree.preorderStr(tree.root); + cout << pre << endl; + } + } + return 0; +} diff --git a/Exercise/Homework4/readme.md b/Exercise/Homework4/readme.md new file mode 100644 index 0000000..fe77f27 --- /dev/null +++ b/Exercise/Homework4/readme.md @@ -0,0 +1,93 @@ +Q1 +``` +通过带空指针信息的先根序列(亦称先序序列)创建二叉树,并进行先根(先序)、中根(中序)、后根(后序)遍历。二叉树结点数据域值为不等于0的整数(可能是正数也可能是负数),空指针用0表示,例如1 5 8 0 0 0 6 0 0表示如下图的二叉树。 + +PA567.jpg + +输入格式: +输入为一组用空格间隔的整数,表示带空指针信息的二叉树先根序列。其中空指针信息用0表示。二叉树结点个数不超过150000,高度不超过6000。输入数据保证二叉树各结点数据值互不相等。 + +输出格式: +输出为3行整数,每个整数后一个空格。第1行为该二叉树的先根序列,第2行为中根序列,第3行为后根序列。 + +输入样例: +1 5 8 0 0 0 6 0 0 +输出样例: +1 5 8 6 +8 5 1 6 +8 5 6 1 +代码长度限制 +16 KB +时间限制 +200 ms +内存限制 +20 MB +栈限制 +8192 KB +``` + +Q2 +``` +编写程序在二叉树中查找给定结点及父结点。二叉树结点的数据域值不等于0的整数。 + +输入格式: +输入第1行为一组用空格间隔的整数,表示带空指针信息的二叉树先根序列,其中空指针用0表示。例如1 5 8 0 0 0 6 0 0表示如下图的二叉树。第2行为整数m,表示查询个数。接下来m行,每行为一个不等于0的整数K,表示要查找的结点的数据值。m不超过100,二叉树结点个数不超过150000,高度不超过6000。输入数据保证二叉树各结点数据值互不相等。 + +PA567.jpg + +输出格式: +输出为m行,每行1个整数,表示被查找结点K的父结点数据值,若二叉树中无结点K或结点K无父结点,则输出0。 + +输入样例: +1 5 8 0 0 0 6 0 0 +3 +8 +1 +6 +输出样例: +5 +0 +1 +代码长度限制 +16 KB +时间限制 +300 ms +内存限制 +20 MB +栈限制 +131000 KB +``` + +Q3 +``` +给定非空二叉树的中根序列和后根序列,请编写程序创建该二叉树,计算其高度和先根序列;如给定的中根和后根序列不合法,则亦能识别。 + +输入格式: +输入包含多组数据(不超过10组),每组为两行字符串,第一行表示某二叉树的后根序列,第二行表示其中根序列。结点的值均为A-Z的大写字母,故二叉树结点个数不超过26,且保证输入的两个序列都是结点的全排列,但不一定是合法的中根和后根序列。输入保证不是空二叉树。 + +输出格式: +对于每组数据,如果输入的序列不合法(不是同一棵树的中根序列和后根序列),则输出INVALID;若输入序列合法,输出为两行,第一行为一个整数,表示该二叉树的高度,第二行为一个字符串,表示该二叉树的先根序列。 + +输入样例1: +CEFDBHGA +CBEDFAGH +CBEDFAGH +CEFDBHGA +BCA +CAB + +输出样例1: +3 +ABCDEFGH +INVALID +INVALID + +代码长度限制 +16 KB +时间限制 +50 ms +内存限制 +64 MB +栈限制 +8192 KB +``` \ No newline at end of file diff --git a/Exercise/Homework5/Q1.cpp b/Exercise/Homework5/Q1.cpp new file mode 100644 index 0000000..b8538cd --- /dev/null +++ b/Exercise/Homework5/Q1.cpp @@ -0,0 +1,151 @@ +#include +#include +#include +#include +using namespace std; + +template +struct Node { + T element; + Node* left; + Node* right; + Node* parent; + + Node(const T& e, Node* p) : element(e), parent(p), left(nullptr), right(nullptr) {} +}; + +template +class BinTree { +public: + Node* root; + int height; + vector arr; + + BinTree() : root(nullptr), height(0) {} + BinTree(const vector& arrs) : arr(arrs), height(0) {} + void test() { + for (auto x : arr) { + cout << x << " "; + } + } + Node* returnroot() { + return this->root; + } + + Node* build(int& index, Node* parent) { + if (index >= arr.size() || arr[index] == 0) { + index++; + return nullptr; + } + + Node* node = new Node(arr[index], parent); + index++; + node->left = build(index, node); + node->right = build(index, node); + + return node; + } + + void buildTree() { + int index = 0; + this->root = build(index, nullptr); + } + + // + void preorder(Node* node) { + if (node == nullptr) return; + + cout << node->element << " "; + preorder(node->left); + preorder(node->right); + } + void inorder(Node* node) { + if (node == nullptr) return; + + inorder(node->left); + cout << node->element << " "; + //cout << ( node->parent == nullptr ? 0 : node->parent->element) << endl; + inorder(node->right); + } + void postorder(Node* node) { + if (node == nullptr) return; + + postorder(node->left); + postorder(node->right); + cout << node->element << " "; + } + Node* inorder2(Node* node, const T& x) { + if (node == nullptr) return nullptr; + + if (node->element == x) { + return node; + } + + Node* leftResult = inorder2(node->left, x); + if (leftResult != nullptr) { + return leftResult; + } + + Node* rightResult = inorder2(node->right, x); + return rightResult; + } + void findparent(const T& e) { + auto x = inorder2(this->root, e); + if (x == nullptr || x->parent == nullptr) cout << 0 << endl; + else cout << x->parent->element << endl; + } + void deletesub(Node* node) { + if (node == nullptr) return; + deletesub(node->left); + deletesub(node->right); + delete node; + } + + bool deletenode(const T& e) { + auto x = inorder2(this->root, e); + if (x == nullptr) return false; + + if (x == this->root) { + deletesub(x); + this->root = nullptr; + return true; + } + + if (x->parent->left == x) { + x->parent->left = nullptr; + } else { + x->parent->right = nullptr; + } + deletesub(x); + return true; + } +}; + +int main() { + int x = 0; + vector arr; + string line; + getline(cin, line); + stringstream ss(line); + int num; + + while (ss >> num) { + arr.push_back(num); + } + BinTree tree(arr); + tree.buildTree(); + + int m; + cin >> m; + for (int i = 0; i < m; i++) { + int t; + cin >> t; + bool flag = tree.deletenode(t); + if (flag) { + tree.inorder(tree.root); + cout << endl; + } + else cout << "0" << endl; + } + return 0; +} diff --git a/Exercise/Homework5/readme.md b/Exercise/Homework5/readme.md new file mode 100644 index 0000000..7a3bec9 --- /dev/null +++ b/Exercise/Homework5/readme.md @@ -0,0 +1,135 @@ +Q1 +``` +编写程序对给定二叉树执行若干次删除子树操作,输出每次删除子树后剩余二叉树的中根序列。二叉树结点的数据域值为不等于0的整数。每次删除操作是在上一次删除操作后剩下的二叉树上执行。 + +输入格式: +输入第1行为一组用空格间隔的整数,表示带空指针信息的二叉树先根序列,其中空指针信息用0表示。例如1 5 8 0 0 0 6 0 0表示如下图的二叉树。第2行为整数m,表示要进行的删除操作次数。接下来m行,每行一个不等于0的整数K,表示要删除以K为根的子树。m不超过100,二叉树结点个数不超过5000。输入数据保证各结点数据值互不相等,且删除子树后二叉树不为空。 + +PA567.jpg + +输出格式: +输出为m行,每行为一组整数,表示执行删除操作后剩余二叉树的中根序列(中根序列中每个整数后一个空格)。若要删除的子树不在当前二叉树中,则该行输出0(0后无空格)。 + +输入样例: +1 5 8 0 0 0 6 0 0 +3 +5 +8 +6 +输出样例: +1 6 +0 +1 +代码长度限制 +16 KB +时间限制 +100 ms +内存限制 +10 MB +栈限制 +8192 KB +``` + +Q2 +``` +已知二叉树结点为不等于0的整数。给定一个整数K,请编写程序找出以根结点为起点叶结点为终点的所有路径中,结点值之和等于K的所有路径。例如K=15,对于下图所示的二叉树t,满足条件的路径有2条,即8-5-2和8-7。若没有满足条件的路径,则亦能识别。 + +img.jpg + +输入格式: +输入为2行,第一行为一组用空格间隔的整数,个数不超过100个,表示带空指针信息的二叉树先根序列,其中空指针信息用0表示。第2行为整数K。 + +输出格式: +输出第一行为一个整数,表示满足条件的路径条数;若没有满足条件的路径,则输出0。从第二行开始,每行为一条满足条件的路径,若有多条满足条件的路径,则按从左到右的顺序依次输出,路径中每个结点值后一个空格,若两条不同的路径包含的各结点值恰好相等,则都需输出。 + +输入样例1: +8 5 1 0 0 2 0 0 7 0 0 +15 +输出样例1: +2 +8 5 2 +8 7 +输入样例2: +-1 2 0 0 3 0 0 +2 +输出样例2: +1 +-1 3 +输入样例3: +1 1 0 0 1 0 0 +2 +输出样例3: +2 +1 1 +1 1 +输入样例4: +-1 2 0 0 3 0 0 +8 +输出样例4: +0 +代码长度限制 +16 KB +时间限制 +50 ms +内存限制 +64 MB +栈限制 +8192 KB +``` + +Q3 +``` +给定一棵非空二叉树,结点数据域值为互不相等且不等于0的正整数,同时给定两个正整数a和b,请编写程序输出数据域值为a和b的两个结点间的路径和路径长度。注意本题的路径不受父子关系约束,只要两个结点间有边,就算路径。例如下图所示二叉树,7和8间的路径为7 5 8,路径长度为2;6和8间的路径为6 3 5 8,路径长度为3;9和8间的路径为9 2 1 3 5 8,路径长度为5。9和1之间的路径为9 2 1,路径长度为2。 + +223.jpg + +输入格式: +输入第一行为一组用空格间隔的整数,表示带空指针信息的二叉树先根序列,二叉树结点个数不超过150000,高度不超过5000。第二行为一个整数T,表示查询数目。接下来T行,每行两个互不相等的正整数a和b,含义如题目所述,保证a和b一定在二叉树中。 + +输出格式: +对于每个查询输出为两行整数。第一行为1个整数,表示两个结点间的路径长度,第二行为1行整数,每个整数后一个空格,表示路径。 + +输入样例1: +1 2 9 0 0 0 3 5 7 0 0 8 0 0 6 0 0 +3 +7 8 +6 8 +9 8 +输出样例1: +2 +7 5 8 +3 +6 3 5 8 +5 +9 2 1 3 5 8 +输入样例2: +1 2 0 4 0 7 0 0 3 5 0 0 6 8 20 0 0 21 0 0 0 +3 +7 8 +5 8 +20 21 +输出样例2: +6 +7 4 2 1 3 6 8 +3 +5 3 6 8 +2 +20 8 21 +输入样例3: +2 4 3 0 0 8 0 0 10 5 7 9 0 0 1 0 0 17 0 0 6 0 0 +3 +8 3 +9 17 +1 6 +输出样例3: +2 +8 4 3 +3 +9 7 5 17 +4 +1 7 5 10 6 +数据规模: +测试点0-2、5-9:结点个数≤50,树高≤20 +测试点3:4500≤结点个数≤5500,4000≤树高≤5000 +测试点4:130000≤结点个数≤150000,10≤树高≤20 +``` \ No newline at end of file