题解 | #二叉树遍历#
二叉树遍历
https://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef
#include <iostream> #include <string> #include<cstring> #include <cstdio> using namespace std; typedef struct Binode { char data; struct Binode* lnode; struct Binode* rnode; Binode(char c): data(c), lnode(NULL), rnode(NULL) {} }; Binode* build(int& pos, string str) { char c = str[pos++]; if (c == '#') return NULL; Binode* b = new Binode(c); b->lnode = build(pos, str); b->rnode = build(pos, str); return b; } void midVisit(Binode* b) { if (b->lnode != NULL) { midVisit(b->lnode); } cout << b->data << " "; if (b->rnode != NULL) { midVisit(b->rnode); } } int main() { string str; while (cin >> str) { // 注意 while 处理多个 case int pos = 0; Binode* b = build(pos, str); midVisit(b); } } // 64 位输出请用 printf("%lld")