几个学校作业
来源:互联网 发布:ios开发程序员招聘 编辑:程序博客网 时间:2024/04/28 23:25
块链的移位
#define CHUNK_SIZE 4/* 可由用户定义的块大小 */
/* 未存储的空间用'#'字符代替 */
typedef struct Chunk
{
char ch[CHUNK_SIZE];
structChunk *next;
}Chunk;
Chunk* insert(Chunk* Shead,Chunk* Thead,char c)
{
Chunk* cur=Shead;
int flag=0;
while(cur)
{
for(int i=0;i<=CHUNK_SIZE-1;i++)
{
if(c==cur->ch[i])
{
flag=i;
break;
}
}
cur=cur->next;
}
Chunk* newone=NULL;
if(flag<CHUNK_SIZE-1)
{
newone=newChunk;
while(flag<=CHUNK_SIZE-1)
{
flag++;
newone->ch[flag]=cur->ch[flag];
cur->ch[flag]='#';
}
}
Chunk* tail=Thead;
while(tail->next)
tail=tail->next;
Chunk* k=NULL;
if(cur->next)
k=cur->next;
cur->next=Thead;
tail->next=newone;
tail=tail->next;
if(tail)
tail->next=k;
else
tail=newone;
return Shead;
}
15. 已知在二叉链表表示的二叉树中,root 为根结点, p↑和q↑为二叉树中两个结点,试编写算法求距离它们最近的共同祖先。
16. 试给出算法求二叉链表表示的二叉树的直径(高度、最大层次数)以及长度等于直径的一条路经(从根到叶子的结点序列)。
17. 试给出算法将二叉树表示的表达式按中序遍历输出并加上相应的扩号。
class Solution15{
public:
void helper(queue<TreeNode*>&fathers,TreeNode* p,TreeNode* root,vector<TreeNode*>&cur)
{
if(root->left&&root->left->val==p->val)//root已经在爸爸队列里,不等
{
for(int i=0;i<=cur.size()-1;i++)
fathers.push(cur[i]);
return;
}
if(root->right&&root->right->val==p->val)
{
for(int i=0;i<=cur.size()-1;i++)
fathers.push(cur[i]);
return;
}
if(root->left){
cur.push_back(root->left);
helper(fathers, p, root->left,cur);
cur.pop_back();
}
if(root->right){
cur.push_back(root->right);
helper(fathers, p, root->right,cur);
cur.pop_back();
}
}
TreeNode* find(TreeNode* root,TreeNode* p,TreeNode* q)
{
queue<TreeNode*> pfathers;
queue<TreeNode*> qfathers;
vector<TreeNode*> curp;
vector<TreeNode*> curq;
curp.push_back(root);
curq.push_back(root);
if(p->val!=root->val)
helper(pfathers, p, root,curp);
if(q->val!=root->val)
helper(qfathers, q, root,curq);
TreeNode* res=NULL;
while(pfathers.front()==qfathers.front())
{
res=pfathers.front();
pfathers.pop();
qfathers.pop();
if(pfathers.empty()||qfathers.empty())
break;
}
return res;
}
};
class Solution16{
public:
vector<vector<TreeNode*>> res;
int height(TreeNode* root)
{
if(!root)return 0;
int l=height(root->left);
int r=height(root->right);
return (l>r? l:r)+1;
}
vector<vector<TreeNode*>> findPath(vector<TreeNode*> &cur,TreeNode* root,int curHeight,int height)
{
if(curHeight>=height)
{
res.push_back(cur);
cout<<"res:"<<endl;
for(int i=0;i<=cur.size()-1;i++)
if(cur[i])
cout<<cur[i]->val;
}
else{
cur.push_back(root->left);
findPath(cur, root->left, curHeight+1, height);
cur.pop_back();
cur.push_back(root->right);
findPath(cur, root->right, curHeight+1, height);
cur.pop_back();
}
returnres;
}
};
class solution17{
public:
string calculate(TreeNode* root)
{
if(!root)return "";
if(root->val>='a'&&root->val<='z')
{
string res;
res+=root->val;
return res;
}
string res;
if(root->val=='*'||root->val=='/')
{
if(root->left&&(root->left->val=='+'||root->left->val=='-'))
{
res+='(';
res+=calculate(root->left);
res+=')';
}
else
res+=calculate(root->left);
res+=(root->val);
if(root->right&&(root->right->val=='+'||root->right->val=='-'))
{
res+='(';
res+=calculate(root->right);
res+=')';
}
else
res+=calculate(root->right);
}
else
res+= calculate(root->left)+(root->val)+calculate(root->right);
return res;
}
};
//测试17
int main()
{
TreeNode* root=new TreeNode('*');
cout<<root->val<<endl;
root->left=new TreeNode('+');
root->right=new TreeNode('+');
root->left->left=new TreeNode('a');
root->left->right=new TreeNode('/');
root->right->left=new TreeNode('e');
root->right->right=new TreeNode('f');
root->left->right->left=new TreeNode('b');
root->left->right->right=new TreeNode('-');
root->left->right->right->left=new TreeNode('e');
root->left->right->right->right=new TreeNode('m');
solution17 s;
cout<<s.calculate(root);
}
- 几个学校作业
- 学校作业-Dp练习
- 12.3学校作业
- 学校作业-Usaco DP水题
- SQL SERVER学校作业的一个文档
- (难度:15%)学校作业之操作系统实验
- 这是学校第八章作业
- 学校.......
- 学校
- 学校
- 商用软件开发绝不同于学校作业
- 商用软件开发绝不同于学校作业
- 商用软件开发绝不同于学校作业
- 深圳多所学校试点电子书包 作业当堂批改
- 中国最小黑客:为不做作业 黑掉学校系统
- 中国最小黑客:为不做作业 黑掉学校系统
- 中国最小黑客:为不做作业 黑掉学校系统
- 中国最小黑客:为不做作业 黑掉学校系统
- 2017年薪酬最高的编程语言
- 我的 10 年自学编程之路
- android第一步,雀氏纸尿裤
- 前端团队协作一点见解
- Python运维基础(3)程序大练习(ATM)
- 几个学校作业
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- 最佳旅游路线
- 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)
- HashMap环 JDK1.7
- 以简单的设计模式代码阐述如何设计委托
- 第七周项目2建立链队算法库
- gensim 中文文本相似度计算
- [hihocoder 1635]Colored Nodes