二叉树的遍历 发表于 2018-04-03 | 分类于 数据结构 二叉树的前中后序遍历,及广度遍历实现 说明:代码使用codeblocks编译,C++实现,个人编写,如有错误,还望指正。 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221#include <iostream>#include <stdlib.h>#include <stack>#include <queue>using namespace std;struct TreeNode{ int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int v):val(v),left(NULL),right(NULL){ }};/*构建这样一棵二叉树: 1 / \ 2 3 /\ /\ 4 5 6 7 / \ 8 9*/void BuildTree(TreeNode* &root){ root=new TreeNode(1); root->left=new TreeNode(2); root->right=new TreeNode(3); root->left->left=new TreeNode(4); root->left->right=new TreeNode(5); root->right->left=new TreeNode(6); root->right->right=new TreeNode(7); root->left->left->left=new TreeNode(8); root->left->right->right=new TreeNode(9); return ;}void DestroyTree(TreeNode* &root){ if(root==NULL) return ; if(root->left) DestroyTree(root->left); if(root->right) DestroyTree(root->right); delete root; return ;}void preRecursionPrint(TreeNode*root){ if(root==NULL) return ; cout<<root->val<<" "; if(root->left) preRecursionPrint(root->left); if(root->right) preRecursionPrint(root->right); return ;}void midRecursionPrint(TreeNode*root){ if(root==NULL) return ; if(root->left) midRecursionPrint(root->left); cout<<root->val<<" "; if(root->right) midRecursionPrint(root->right); return ;}void afterRecursionPrint(TreeNode*root){ if(root==NULL) return ; if(root->left) afterRecursionPrint(root->left); if(root->right) afterRecursionPrint(root->right); cout<<root->val<<" "; return ;}void preNonRecursionPrint(TreeNode*root){ if(root==NULL) return ; TreeNode * cur=root; stack<TreeNode*>s; while(cur) { cout<<cur->val<<" "; if(cur->right) { s.push(cur->right); } if(cur->left) cur=cur->left; else if(!s.empty()) { cur=s.top(); s.pop(); } else return ; } return ;}void midNonRecursionPrint(TreeNode*root){ if(root==NULL) return ; TreeNode * cur=root; stack<TreeNode*>s; while(cur || (!s.empty())) { while(cur) { s.push(cur); cur=cur->left; } if(!s.empty()) { cur=s.top(); s.pop(); cout<<cur->val<<" "; if(cur->right) cur=cur->right; else cur=NULL; } else return ; } return ;}void afterNonRecursionPrint(TreeNode*root){ if(root==NULL) return ; TreeNode * cur=root; stack<TreeNode*>s1; stack<TreeNode*>s2; while(cur || (!s1.empty())) { while(cur) { s1.push(cur); s2.push(cur); cur=cur->right; } cur = s1.top(); s1.pop(); cur =cur->left; } while(!s2.empty()) { cur=s2.top(); s2.pop(); cout<<cur->val<<" "; } return ;}void widePrint(TreeNode*root){ if(root==NULL) return ; queue<TreeNode*>q; q.push(root); TreeNode*cur; while(!q.empty()) { cur=q.front(); if(cur) cout<<cur->val<<" "; q.pop(); if(cur->left) q.push(cur->left); if(cur->right) q.push(cur->right); } return ;}int main(){ TreeNode *root; //构建二叉树 BuildTree(root); preRecursionPrint(root); cout<<endl; preNonRecursionPrint(root); cout<<endl; midRecursionPrint(root); cout<<endl; midNonRecursionPrint(root); cout<<endl; afterRecursionPrint(root); cout<<endl; afterNonRecursionPrint(root); cout<<endl; widePrint(root); cout<<endl; //销毁二叉树 DestroyTree(root); return 0;} 123456789运行结果:1 2 4 8 5 9 3 6 71 2 4 8 5 9 3 6 78 4 2 5 9 1 6 3 78 4 2 5 9 1 6 3 78 4 9 5 2 6 7 3 18 4 9 5 2 6 7 3 11 2 3 4 5 6 7 8 9