二叉树的非递归遍历
二叉树的遍历我想大家都知道了,主要有先序、中序、后序,递归的遍历我就不说了,今天小编给大家主要介绍下二叉树的非递归遍历。 节点结构体: #include<stack> enum tag { L, R }; template<typename T>//可以变成类 struct BintNode { BintNode():left(nullptr),right(nullptr),value(0){} BintNode(T v) { left = nullptr; right = nullptr; value = v; } T value; BintNode<T> *left; BintNode<T> *right; }; template<typename E> struct stkNode { stkNode(BintNode<E>*N=nullptr):ptr(N), r(L){} BintNode<E>*ptr; tag r; }; 首先是先序遍历,借助栈实现的。 void nonrepreorder(BintNode<Y>*root)const//非递归先序 { if (root) { stack<BintNode<Y>*> bitr; bitr.push(root); BintNode<Y>*p; while (!bitr.empty()) { p = bitr.top();