题解 | #二叉树遍历#
二叉树遍历
https://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef
#include<iostream> #include<string> using namespace std; typedef struct TreeNode{ char val; struct TreeNode* left; struct TreeNode* right; }Tree; int idx = 0; string str; void createTree(Tree* &root) { if(str[idx] == '#'){ idx++; root = nullptr; return; } if(root == nullptr){ root = new Tree(); root -> val = str[idx++]; } createTree(root -> left); createTree(root -> right); } void suffixOrder(Tree* root) { if(root == nullptr) return; suffixOrder(root -> left); cout << root -> val << " "; suffixOrder(root -> right); } int main(void) { cin >> str; Tree* root = nullptr; createTree(root); suffixOrder(root); return 0; }