20170926_二叉树遍历_递归
//二叉树遍历_递归
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<numeric>
#include<functional>
using namespace std;
struct BinTreeNode
{
int value;
BinTreeNode *pLeft;
BinTreeNode *pRight;
BinTreeNode(int x):value(x),pLeft(nullptr),pRight(nullptr) {};
};
/*前序遍历——递归*/
void PreOrder(BinTreeNode *root)
{
if(root==nullptr)
return;
else
{
cout<<root->value<<","; //访问根节点
PreOrder(root->pLeft); //递归遍历左孩子
PreOrder(root->pRight); //递归遍历右孩子
}
}
/*中序遍历——递归*/
void InOrder(BinTreeNode *root)
{
if(root==nullptr)
return;
else
{
InOrder(root->pLeft); //递归遍历左孩子
cout<<root->value<<","; //访问根节点
InOrder(root->pRight); //递归遍历右孩子
}
}
/*后序遍历——递归*/
void PostOrder(BinTreeNode *root)
{
if(root==nullptr)
return;
else
{
PostOrder(root->pLeft); //递归遍历左孩子
PostOrder(root->pRight); //递归遍历右孩子
cout<<root->value<<","; //访问根节点
}
}