文章

50

粉丝

0

获赞

54

访问

1.4k

头像
二叉树遍历 题解:CreateBT(BiTree &T,int &pos,string &s)!
P1161 清华大学/南京大学机试题
发布于2026年3月11日 20:39
阅读数 37

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

typedef struct BTNode {
	char c;
	struct BTNode* lc, * rc;
}BTNode, * BiTree;
void CreateBT(BiTree& T, int& pos, string &s)
{
	if (pos >= s.size())
		return;
	char temp;
	temp = s[pos++];
	
	if (temp == '#')
		T = NULL;
	else {
		T = new BTNode;
		T->c = temp;
		T->lc = NULL; 
		T->rc = NULL; 
		CreateBT(T->lc, pos, s);
		CreateBT(T->rc, pos, s);
	}
}
void InOrder(BiTree T)
{
	if (T != NULL)
	{
		InOrder(T->lc);
		cout << T->c << " ";
		InOrder(T->rc);
	}
}
void DeleteBT(BiTree &T)
{
	if(T)
	{
		DeleteBT(T->lc);
		DeleteBT(T->rc);
		delete T;
	}
}
int main()
{
	string s;
	while (cin >> s)
	{
		int pos = 0;
		BiTree T;
		CreateBT(T, pos, s);
		InOrder(T);
		cout << endl;
		DeleteBT(T);
	}
	return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发