二叉树
思路:建立二叉树,左旋90度输出可以用一个逆中序遍历和一个参数记录当前节点所在层数的方式来输出#include <iostream>#include <cstring>#include <stdlib.h>#include <stack>#include <cstdio&a
·
思路:建立二叉树,左旋90度输出可以用一个逆中序遍历和一个参数记录当前节点所在层数的方式来输出
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <stack>
#include <cstdio>
using namespace std;
typedef struct Node
{
struct Node* lch;
struct Node* rch;
string data;
} Node;
Node *root;
string s;
Node *CreateTree() ///建立二叉树
{
cin>>s;
Node *root = new Node;
if(s[0]=='#')
{
root=NULL;
return NULL;
}
root->data.assign(s);
root->lch = CreateTree();
root->rch = CreateTree();
return root;
}
void Traversal(Node *root,int cur) ///遍历
{
if(root==NULL)
return;
Traversal(root->rch,cur+1);
for(int i=0;i<cur;i++)
printf(" ");
cout<<root->data<<endl;
Traversal(root->lch,cur+1);
}
void Clear(Node *root) ///清空树
{
if(root)
{
Clear(root->lch);
Clear(root->rch);
delete root;
root=NULL;
}
}
int main()
{
for(int i=0; i<10; i++)
{
Node *root=CreateTree();
Traversal(root,0);
cout<<endl;
Clear(root);
}
return 0;
}

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)