当前位置: 代码迷 >> 综合 >> 新二叉树 p1305——洛谷
  详细解决方案

新二叉树 p1305——洛谷

热度:63   发布时间:2024-01-13 17:01:30.0

题目链接

注意题目不是按照广度优先给出节点的,比如a下面左b右c,有可能下一个会给c的信息而不是b的


#include <iostream>using namespace std;
typedef struct t{
    char c;t *right = NULL;t *left = NULL;
}tree,*Tree;
Tree createTree(){
    Tree Trees = new tree;return Trees;
}
Tree Treearr[1000] = {
    NULL};
int mark = 0;void getTree(int num,Tree &ance){
    char char1,char2,char3;for(int i=0;i<num;i++){
    cin >> char1 >> char2 >> char3;if(i==0){
    Treearr[char1] = new tree;ance = Treearr[char1];Treearr[char1]->c = char1;}mark = (int)char1;if(char2 != '*'){
    Treearr[char2] = Treearr[mark]->left = new tree;Treearr[mark]->left->c = char2;Treearr[mark]->left->left = Treearr[mark]->left->right = NULL;}if(char3 != '*'){
    Treearr[char3] = Treearr[mark]->right = new tree;Treearr[mark]->right->c = char3;Treearr[mark]->right->left = Treearr[mark]->right->right = NULL;}}}
void putTree(Tree ance){
    if(ance == NULL){
    return ;}cout << ance->c;putTree(ance->left);putTree(ance->right);
}
int main()
{
    Tree ancestor;int num;cin >> num;getTree(num,ancestor);putTree(ancestor);
}