博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二叉树的遍历
阅读量:4310 次
发布时间:2019-06-06

本文共 1449 字,大约阅读时间需要 4 分钟。

二叉树的遍历可以使用递归,循环的方式遍历,一下列出了使用栈和队列的方式循环遍历二叉树的方式。

#include "stdafx.h"#include 
#include
using namespace std;struct TreeNode{ TreeNode() { memset(this, 0, sizeof(*this)); } TreeNode* pLeft; TreeNode* pRight; int m_nValue;};/* 递归遍历 */void TreeOrder(TreeNode* root){ if (NULL == root) { return ; } printf("%d ", root->m_nValue); TreeOrder(root->pLeft); TreeOrder(root->pRight);}/* 使用栈循环遍历 */void TreeOrderStack(TreeNode* root){ if (NULL == root) { return ; } stack
s; s.push(root); while (!s.empty()) { TreeNode* pNode = s.top(); s.pop(); printf("%d ",pNode->m_nValue); if (NULL != pNode->pLeft) { s.push(pNode->pLeft); } if (NULL != pNode->pRight) { s.push(pNode->pRight); } }}/* 使用队列循环遍历 */void TreeOrderQueue(TreeNode* root){ if (NULL == root) { return ; } queue
q; q.push(root); while (!q.empty()) { TreeNode* pNode = q.front(); q.pop(); printf("%d ", pNode->m_nValue); if (NULL != pNode->pLeft) { q.push(pNode->pLeft); } if (NULL != pNode->pRight) { q.push(pNode->pRight); } }}int _tmain(int argc, _TCHAR* argv[]){ return 0;}

 

转载于:https://www.cnblogs.com/jiangwang2013/p/3625981.html

你可能感兴趣的文章
vs下dll与exe调试
查看>>
排列——递归
查看>>
cocos2d lua的cclog 在logcat中显示
查看>>
雅礼学习10.4
查看>>
Dota2 荒神罪 破解
查看>>
TP控制器的操作
查看>>
Installing Oracle Database 12c Release 2(12.2) RAC on RHEL7.3 in Silent Mode
查看>>
使用Metasploit进行端口扫描
查看>>
[LeetCode] 258. Add Digits
查看>>
python3.5+ asyncio await异步详解
查看>>
android手机获取手机号
查看>>
Android和WCF通信 - 大数据压缩后传输
查看>>
TC中HTB的使用备注
查看>>
深入学习Redis(1):Redis内存模型
查看>>
当Eclipse爱上SVN
查看>>
hdu 4586 Play the Dice (概率+等比数列)
查看>>
阿里云api调用做简单的cmdb
查看>>
软考笔记
查看>>
ORACLE 日期函数
查看>>
【Java基础总结】数据库编程
查看>>