Three kinds of traversal of binary tree

Source: Internet
Author: User



1, the first sequence traversal: First order traversal is the first output root node, and then output the left subtree, and finally output the right subtree. The result of the first order traversal is: ABCDEF

2, the middle sequence traversal: the middle sequence traversal is the first output of the left subtree, and then output the root node, and finally output the right sub-tree. The result of the middle sequence traversal is: CBDAEF

3, after the post-traversal: The post-traversal is the first output of the left subtree, and then output the right subtree, the final output root node. The result of the post-sequential traversal is: CDBFEA


#include <stdio.h>#include <stdlib.h>typedefchar TelemType;typedefstruct TNode{    TelemType data;    structTNode *lchild,*rchild;} BitNode;//声明BitNode* createTree(void);voidpreOrderTraverse(BitNode *);voidinOrderTraverse(BitNode *);voidlastOrderTraverse(BitNode *);int main(int agrc,char*argv[]){    BitNode *root=NULL;    root=createTree();     printf("\n先序遍历二叉树:");    preOrderTraverse(root);    printf("\n中序遍历二叉树:");    inOrderTraverse(root);    printf("\n后序遍历二叉树:");    lastOrderTraverse(root);        return0;}//创建二叉树BitNode* createTree(void){    BitNode *b;    TelemType ch;    scanf("%c",&ch);    if(ch==‘#‘){        b=NULL;    }else{      b=(BitNode *)malloc(sizeof(BitNode))       b->data=ch;        b->lchild=createTree();        b->rchild=createTree();    }    returnb;}//先序遍历voidpreOrderTraverse(BitNode *root){    if(root){        printf("%c",root->data);        preOrderTraverse(root->lchild);             preOrderTraverse(root->rchild);         }}//中序遍历voidinOrderTraverse(BitNode *root){    if(root){        inOrderTraverse(root->lchild);        printf("%c",root->data);        inOrderTraverse(root->rchild);    }}//后序遍历voidlastOrderTraverse(BitNode *root){    if(root){        lastOrderTraverse(root->lchild);        lastOrderTraverse(root->rchild);        printf("%c",root->data);    }}

Results:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Three kinds of traversal of binary tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.