zoukankan      html  css  js  c++  java
  • 三种递归遍历二叉树

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 typedef int ElementType;
     5 
     6 struct BinarySearchTreeNode
     7 {
     8     ElementType Element;
     9     struct BinarySearchTreeNode *Left;
    10     struct BinarySearchTreeNode *Right;
    11 };
    12 
    13 int BinarySearchTreePreOrder(struct BinarySearchTreeNode *TreeRoot)
    14 {
    15     if(TreeRoot!=NULL)
    16     {
    17         printf("%d
    ",TreeRoot -> Element);
    18         BinarySearchTreePreOrder(TreeRoot->Left);
    19         BinarySearchTreePreOrder(TreeRoot->Right);
    20     }
    21     return 0;
    22 }
    23 
    24 int BinarySearchTreeInOrder(struct BinarySearchTreeNode *TreeRoot)
    25 {
    26     if(TreeRoot!=NULL)
    27     {
    28         BinarySearchTreeInOrder(TreeRoot->Left);
    29         printf("%d
    ",TreeRoot -> Element);
    30         BinarySearchTreeInOrder(TreeRoot->Right);
    31     }
    32     return 0;
    33 }
    34 
    35 int BinarySearchTreePostOrder(struct BinarySearchTreeNode *TreeRoot)
    36 {
    37     if(TreeRoot!=NULL)
    38     {
    39         BinarySearchTreePostOrder(TreeRoot->Left);
    40         BinarySearchTreePostOrder(TreeRoot->Right);
    41         printf("%d
    ",TreeRoot -> Element);
    42     }
    43     return 0;
    44 }
    45 
    46 int main()
    47 {
    48 //    BinarySearchTreePreOrder(TreeRoot);
    49 //    BinarySearchTreeInOrder(TreeRoot);
    50     BinarySearchTreePostOrder(TreeRoot);
    51     return 0;
    52 }
  • 相关阅读:
    poj 1200 crasy search
    cdoj 1092 韩爷的梦
    fzu 2257 saya的小熊饼干
    zoj 3950 how many nines
    zoj 3963 heap partion
    fzu 2256 迷宫
    fzu 2253 salty fish
    hdu 2473 Junk-Mail Filter
    codeforces 129B students and shoes
    hdu 3367 Pseudoforest
  • 原文地址:https://www.cnblogs.com/Asurudo/p/9427455.html
Copyright © 2011-2022 走看看