二叉树的遍历
来源:互联网 发布:淘宝网页商品拍照制作 编辑:程序博客网 时间:2024/06/05 07:02
先序遍历
void PreorderTraversal( BinTree BT ){ if (BT){ printf(" %c",BT->Data); PreorderTraversal(BT->Left); PreorderTraversal(BT->Right); }}
中序遍历
void InorderTraversal( BinTree BT ){ if (BT){ InorderTraversal(BT->Left); printf(" %c",BT->Data); InorderTraversal(BT->Right); }}
后序遍历
void PostorderTraversal( BinTree BT ){ if (BT){ PostorderTraversal(BT->Left); PostorderTraversal(BT->Right); printf(" %c",BT->Data); }}
层次遍历
(数组实现)
void LevelorderTraversal( BinTree BT ){ if (BT){ BinTree Q[100]; BinTree p; int head=0,tail=0; if (BT){ Q[tail++]=BT; while (tail!=head){ p=Q[head++]; printf(" %c",p->Data); if (p->Left) Q[tail++]=p->Left; if (p->Right) Q[tail++]=p->Right; } } }}
(队列实现)
void LevelOrder(Node *root) { if(root == NULL) return; queue<Node*> Q; Q.push(root); //while there is at least one discovered node while(!Q.empty()) { Node* current = Q.front(); Q.pop(); // removing the element at front cout<<current->data<<" "; if(current->left != NULL) Q.push(current->left); if(current->right != NULL) Q.push(current->right); } }
0 0
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 反向代理服务器的工作原理
- 设计模式 状态模式 以自动售货机为例
- Java成员变量的初始化
- 使用面向对象重构之-继承中的抽象—模板方法
- 11 Linux mv
- 二叉树的遍历
- java学习日志(一)-- String、Integer、int之间的类型转换
- 99乘法表
- 学习设计模式(7)——6大原则
- Ansible之 Inventory 资源清单介绍
- 算法提高 学霸的迷宫
- 设计模式之--代理模式
- 面试题 第三弹
- 碉堡了!程序员用深度学习写了个老板探测器(附源码)