复习二叉树的前中后序遍历非递归,有完整代码,也有在leetcode上的函数部分,第一次用vector
另外,->和.是不一样的
比如,struct p p.a
struct *p p->a
关于bool isLeaf()const
这个是const成员函数,为了维持程序的健壮性
保证了函数在执行过程中不会修改数据成员,也不允许调用非const成员函数
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int>list;
TreeNode*current=root;
int count=0;
int p[200];
while(!st.empty()||current!=NULL)
{
while(current!=NULL)
{
st.push(current);
list.push_back(current->val);
//p[count++]=current->val;
current=current->left;
}
if(!st.empty())
{
current=st.top();
st.pop();
current=current->right;
}
}
return list;
//return p;
}
};
完整的三种遍历的代码,后序遍历比较麻烦,加isFirst进行判断
#include<stdio.h>
#include<iostream>
#include<stack>
using namespace std;
template<class T>
class BinaryTree;
template<class T>
class TreeNode
{
friend class BinaryTree<T>;
public:
T data;
bool isFirst;
TreeNode<T>*lchild;
TreeNode<T>*rchild;
TreeNode(T da,TreeNode<T>*l=NULL,TreeNode<T>*r=NULL)
{
data=da;
lchild=l;
rchild=r;
isFirst=true;
}
bool isLeaf()const;
void visit();
};
template<class T>
bool TreeNode<T>::isLeaf()const
{
if(lchild==NULL && rchild==NULL)
return true;
else return false;
}
template<class T>
void TreeNode<T>::visit()
{
cout<<"访问"<<this->data<<endl;
}
template<class T>
class BinaryTree//class后面不带括号
{
private:
TreeNode<T>*root;
public:
BinaryTree();
~BinaryTree();
bool IsEmpty();
void Distroy(TreeNode<T>*root);
TreeNode<T>*getRoot(){return root;}
TreeNode<T>*create();
void PreOrder(TreeNode<T>*root);
void InOrder(TreeNode<T>*root);
void PostOrder(TreeNode<T>*root);
void FeiPreOrder();
void FeiInOrder();
void FeiPostOrder();
};
template<class T>
BinaryTree<T>::BinaryTree()
{
root=new TreeNode<T>(0);
root=this->create();
}
template<class T>
BinaryTree<T>::~BinaryTree()
{
//暂时忽略
}
/*template<class T>
void BinaryTree<T>::Destroy(TreeNode<T>*p)
{
;
}*/
template<class T>
bool BinaryTree<T>::IsEmpty()
{
if(root==NULL)
return true;
else
return false;
}
template<class T>
TreeNode<T>*BinaryTree<T>::create()
{
root->data='A';
root->lchild=new TreeNode<T>('B');
root->rchild=new TreeNode<T>('C');
root->lchild->lchild=new TreeNode<T>('D');
root->lchild->rchild=new TreeNode<T>('E');
root->rchild->lchild=new TreeNode<T>('F');
root->rchild->rchild=new TreeNode<T>('G');
return root;
}
template<class T>
void BinaryTree<T>::PreOrder(TreeNode<T>*root)
{
//cout<<"先序遍历"<<endl;
if(root!=NULL)
{
root->visit();
PreOrder(root->lchild);
PreOrder(root->rchild);
}
}
template<class T>
void BinaryTree<T>::InOrder(TreeNode<T>*root)
{
//cout<<"中序遍历"<<endl;
if(root!=NULL)
{
InOrder(root->lchild);
root->visit();
InOrder(root->rchild);
}
}
template<class T>
void BinaryTree<T>::PostOrder(TreeNode<T>*root)
{
//cout<<"后序遍历"<<endl;
if(root!=NULL)
{
PostOrder(root->lchild);
PostOrder(root->rchild);
root->visit();
}
}
template<class T>
void BinaryTree<T>::FeiPreOrder()
{
stack<TreeNode<T>*>st;
TreeNode<T>*current=this->root;
while(!st.empty()||current!=NULL)
{
while(current!=NULL)
{
current->visit();
st.push(current);
current=current->lchild;
}
if(!st.empty())
{
current=st.top();
st.pop();
current=current->rchild;
}
}
}
template<class T>
void BinaryTree<T>::FeiInOrder()
{
stack<TreeNode<T>*>st;
TreeNode<T>*current=root;
while(!st.empty()||current!=NULL)
{
while(current!=NULL)
{
st.push(current);
current=current->lchild;
}
if(!st.empty())
{
current=st.top();
st.pop();
current->visit();
current=current->rchild;
}
}
}
template<class T>
void BinaryTree<T>::FeiPostOrder()
{
stack<TreeNode<T>*>st;
TreeNode<T>*current=root;
while(!st.empty()||current!=NULL)
{
while(current!=NULL)
{
st.push(current);
current=current->lchild;
}
if(!st.empty())
{
current=st.top();
if(current->isFirst==true)
{
current->isFirst=false;
current=current->rchild;
}
else
{
current->visit();
st.pop();
current=NULL;//这个里面因为我前面的current被改了,所以不是null了,要恢复null实现从栈里面接着寻找元素
//current=st.top();
}
}
}
}
int main()
{
BinaryTree<char> btree;
cout<<"前序遍历结果"<<endl;
btree.PreOrder(btree.getRoot());
cout<<endl;
btree.FeiPreOrder();
cout<<"中序遍历结果"<<endl;
btree.InOrder(btree.getRoot());
cout<<endl;
btree.FeiInOrder();
cout<<"后序遍历结果"<<endl;
btree.PostOrder(btree.getRoot());
cout<<endl;
btree.FeiPostOrder();
return 0;
}
然后还有一个不修改定义的后序遍历,后序比前中麻烦的地方是需要考虑到各种缺孩子的情况
leetcode上后序遍历已经ac的代码
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
TreeNode*prev;
TreeNode*current=root;
stack<TreeNode*>st;
vector<int>list;
while(!st.empty()||current!=NULL)
{
while(current!=NULL)
{
st.push(current);
prev=current;
current=current->left;
}
if(!st.empty())
{
current=st.top();
if(current->left==NULL && current->right!=NULL)
{
if(prev==current->right)
{
list.push_back(current->val);
st.pop();
prev=current;
current=NULL;
}
else
current=current->right;
}
else if(prev!=current->left||current->right==NULL)
{
list.push_back(current->val);
st.pop();
prev=current;
current=NULL;
}
else
{
current=current->right;
}
}
}
return list;
}
};
看了一个大佬的文章写的很清晰,复习神器
https://www.cnblogs.com/hapjin/p/5679482.html