图形:
程序:
#include<stdio.h> #include<stdlib.h> struct node{ char data; struct node* left; struct node* right; }; struct node* newNode(char data){ struct node* node = (struct node*)malloc(sizeof(struct node)); node->data=data; node->left=NULL; node->right=NULL; return node; } void printPostorder(struct node* node){ if(node == NULL) return; printPostorder(node->left); printPostorder(node->right); printf("%c ",node->data); } void printInorder(struct node* node){ if(node==NULL){ return; } printInorder(node->left); printf("%c ",node->data); printInorder(node->right); } void printPreorder(struct node* node){ if(node==NULL){ return; } printf("%c ",node->data); printPreorder(node->left); printPreorder(node->right); } int main(){ struct node *root=newNode('A'); root->left=newNode('B'); root->right=newNode('C'); root->left->left=newNode('D'); root->left->right=newNode('E'); root->right->left=newNode('F'); root->right->right=newNode('G'); root->left->left->left=newNode('H'); root->left->left->right=newNode('I'); root->left->right->left=newNode('J'); root->left->right->right=newNode('K'); root->right->left->left=newNode('L'); root->right->left->right=newNode('M'); root->right->right->left=newNode('N'); root->right->right->right=newNode('O'); printf(" Preorder raversal of binary tree is "); printPreorder(root); printf(" Inorder raversal of binary tree is "); printInorder(root); printf(" Postorder raversal of binary tree is "); printPostorder(root); return 0; }
输出:
Preorder raversal of binary tree is A B D H I E J K C F L M G N O Inorder raversal of binary tree is H D I B J E K A L F M C N G O Postorder raversal of binary tree is H I D J K E B L M F N O G C A