// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
struct BitreeNode
{
int data;
struct BitreeNode *lchild, *rchild;
};
void InitTreeNode(BitreeNode &t,int data,BitreeNode *lchild,BitreeNode *rchild)
{
t.data = data;
t.lchild = lchild;
t.rchild = rchild;
}
//前序
void PreOrder(BitreeNode *t)
{
if (t != nullptr)
{
cout << t->data << endl;
PreOrder(t->lchild);
PreOrder(t->rchild);
}
}
//中序
void Inorder(BitreeNode *t)
{
if (t != nullptr)
{
Inorder(t->lchild);
cout << t->data << endl;
Inorder(t->rchild);
}
}
//后序
void PostOrder(BitreeNode *t)
{
if (t != nullptr)
{
PostOrder(t->lchild);
PostOrder(t->rchild);
cout << t->data << endl;
}
}
//前序非递归
void PreOrder2(BitreeNode *t)
{
stack<BitreeNod
c++ 树递归、非递归前、中、后序遍历及层次遍历
最新推荐文章于 2025-03-21 08:49:22 发布