讨论 / C++题解
Kapi 2017-03-07 21:03:38
点我顶贴 收藏 删除
#include <iostream>

#include <math.h>

using namespace std;

struct BinaryTreeNode

{

char FBI;

BinaryTreeNode* Left;

BinaryTreeNode* Right;

};

char judge(char* data,int M)

{

if (data[0] == '0')

{

for (int i = 1; i < M; i++)

{

if (data[i] == '1')

return 'F';

}

return 'B';

}

else

{

for (int i = 1; i < M; i++)

{

if (data[i] == '0')

return 'F';

}

return 'I';

}

}

BinaryTreeNode* CreatTreeNode(char* data,int N)

{

BinaryTreeNode* gen=new BinaryTreeNode;

int M = pow(2, N-1);

gen->FBI = judge(data, 2*M);

if (N == 0)

{

gen->Left = NULL;

gen->Right = NULL;

}

else

{

char* data_right = data + M;

gen->Left = CreatTreeNode(data, N - 1);

gen->Right = CreatTreeNode(data_right, N - 1);

}

return gen;

}

void PostOrderTraverse(BinaryTreeNode * pRoot)

{

if (pRoot == NULL)

return;

PostOrderTraverse(pRoot->Left); // 后序遍历左子树

PostOrderTraverse(pRoot->Right); // 后序遍历右子树

cout<<pRoot->FBI; // 访问根节点

}

int main()

{

int N;

cin >> N;

char* data = new char[(int)pow(2, N)];

cin >> data;

BinaryTreeNode* gen = CreatTreeNode(data, N);

PostOrderTraverse(gen);

cout << endl;

delete[] data;

return 0;

}

查看更多回复
提交回复