1. 程式人生 > >三種遞歸遍歷二叉樹

三種遞歸遍歷二叉樹

tree binary root sea inorder 遍歷二叉樹 二叉樹 struct !=

 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\n",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\n",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\n",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 }

三種遞歸遍歷二叉樹