#include <iostream>
#include <stack>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
#define ITE
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int v) : val(v), left(NULL), right(NULL) {}
};
#ifdef REC
void preorder(TreeNode* root)
{
if (!root) return;
cout << root->val << " ";
preorder(root->left);
preorder(root->right);
}
void inorder(TreeNode* root)
{
if (!root) return;
inorder(root->left);
cout << root->val << " ";
inorder(root->right);
}
void postorder(TreeNode* root)
{
if (!root) return;
postorder(root->left);
postorder(root->right);
cout << root->val << " ";
}
#else if (define ITE)
void preorder(TreeNode* root)
{
if (!root) return;
stack<TreeNode*> sta;
sta.push(root);
while (!sta.empty())
{
TreeNode* temp = sta.top();
sta.pop();
cout <<
二叉树三种遍历方式
最新推荐文章于 2022-10-23 11:45:06 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)