• 满二叉树遍历


    图形:

    程序:

    #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
  • 相关阅读:
    Notepad++ 配置python快捷键运行方法
    Python 安装setuptools方法
    Python 安装selenium方法
    Sublime 的汉化以及原因
    PHP书籍推荐
    PHP 语言结构(Language constructs)和函数的区别 (转)
    程序员问答网站:StackOverflow
    安装SQL SEVER 2005中的两个常见问题
    致IT同仁 —— IT人士常犯的17个职场错误
    PHP书写规范 PHP Coding Standard
  • 原文地址:https://www.cnblogs.com/litifeng/p/10662120.html
Copyright © 2020-2023  润新知