赞
踩
时间限制: 1 Sec 内存限制: 32 MB
提交: 1816 解决: 1117
ABC DE G F
A B C D E G F C B E G D F A C B E G D F A
- #include<iostream>
- using namespace std;
- struct tree
- {
- tree* left;
- char data;
- tree* right;
- }*head = NULL;
- void creattree(tree*& p)
- {
- char ch = getchar();
- if (ch == ' ')
- {
- p = NULL;
- return;
- }
- else
- {
- p = new tree;
- if (head == NULL)head = p;
- p->data = ch;
- creattree(p->left);
- creattree(p->right);
- }
- }
- void preOrder(tree* head)
- {
- if (head != NULL)
- {
- cout << head->data << " ";
- preOrder(head->left);
- preOrder(head->right);
- }
- }
- void inOrder(tree* head)
- {
- if (head != NULL)
- {
- inOrder(head->left);
- cout << head->data << " ";
- inOrder(head->right);
- }
- }
- void postOrder(tree* head)
- {
- if (head != NULL)
- {
- postOrder(head->left);
- postOrder(head->right);
- cout << head->data << " ";
- }
- }
- int main()
- {
- tree* p = head;
- creattree(p);
- preOrder(head); cout << endl;
- inOrder(head); cout << endl;
- inOrder(head); cout << endl;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。