#include <iostream>
#include <stack>
#include <queue>
using namespace std;
typedef int TreeNodeElem;
struct TreeNode
{
TreeNode* left;
TreeNode* right;
TreeNodeElem elem;
};
//递归遍历
void inOrder_R(TreeNode* root, int(*visit)(TreeNode*))
{
if (root == nullptr)
{
return;
}
inOrder_R(root->left, visit);
visit(root);
inOrder_R(root->right, visit);
}
//非递归遍历
void inOrder(TreeNode* root, int(*visit)(TreeNode*))
{
TreeNode* p = root;
stack<TreeNode*> s;
while (!s.empty() || p != nullptr)
{
if (p != nullptr)
{
s.push(p);
p = p->left;
}
else
{
p = s.top();
s.pop();
visit(p);
p = p->right;
}
}
}
二叉树的遍历(2):中序遍历
最新推荐文章于 2022-10-11 11:32:27 发布