文章

75

粉丝

0

获赞

147

访问

8.7k

头像
二叉树遍历 题解:c++
P1161 清华大学/南京大学机试题
发布于2026年2月26日 16:44
阅读数 37

#include <bits/stdc++.h>
using namespace std;

string s;
int i;

typedef struct Node{
    char data;
    Node *left,*right;
}*Tree;

void CreateTree(Tree &t){
    if(i >= s.size()){
      t = nullptr;
      return;  
    } 
    char ch = s[i++];
    if(ch == '#') t = nullptr;
    else{
        t = new Node();
        t->data = ch;
        CreateTree(t->left);
        CreateTree(t->right);
    }
}

void In(Tree t){
    if(t!=nullptr){
        In(t->left);
        cout << t->data << " ";
        In(t->right);
    }
}

int main(){
    while(cin >> s){
        i = 0;
  &...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发