void PreOrder(Node *node)
{
stack<node *> s;
s.push(node);
Node *p = node;
while ( p!=NULL || !s.empty())
{
while (p)
{
visit(p->value);
s.push(p);
p = p->left;
}
if (!s.empty())
{
p = s.pop();
p = p->right;
}
}
}
void PreOrder2(Node *node)
{
stack<node *> s;
s.push(node);
Node *p = node;
while (!s.empty())
{
p = s.pop();
visit(p->value);
if (p->right)
{
s.push(p->right);
}
if (p->left)
{
s.push(p->left);
}
}
}
前序遍历的非递归实现~(两种方法)
最新推荐文章于 2024-05-26 17:37:29 发布