#include<iostream> #include<vector> using namespace std; //二叉树的定义 struct TreeNode { char val; TreeNode *left; TreeNode *right; TreeNode(int x=‘#‘) : val(x), left(NULL), right(NULL) {} }; //建立二叉树 //AB#D##C##前序遍历 void erchashu_jianli(TreeNode** node) { char val; cin>>val; if(val==‘#‘) { *node=NULL; return; } else { *node=(TreeNode*)malloc(sizeof(TreeNode)); (*node)->val=val; erchashu_jianli(&(*node)->left); erchashu_jianli(&(*node)->right); } return; } //二叉树前序遍历 void qianxu_bianli(TreeNode* root) { if(root!=NULL) cout<<root->val<<‘ ‘; else return; qianxu_bianli(root->left); qianxu_bianli(root->right); return; } int main(int argc,char**argv) { TreeNode* ptr; erchashu_jianli(&ptr); qianxu_bianli(ptr); system("pause"); return 1; }
时间: 2024-11-02 01:50:31