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