使用堆栈的顺序遍历的正确性

我正在编写的没有递归的有序遍历代码如下:

// Iterative function to perform in-order traversal of the tree
void inorderIterative(Node *root)
{
    // create an empty stack
    stack<Node*> stack;

    // start from root node (set current node to root node)
    Node *curr = root;

    // if current node is null and stack is also empty,we're done
    while (!stack.empty() || curr != nullptr)
    {
        // if current node is not null,push it to the stack (defer it)
        // and move to its left child
        if (curr != nullptr)
        {
            stack.push(curr);
            curr = curr->left;
        }
        else
        {
            // else if current node is null,we pop an element from stack,// print it and finally set current node to its right child
            curr = stack.top();
            stack.pop();
            cout << curr->data << " ";

            curr = curr->right;
        }
    }}

现在,我们需要证明代码的正确性。有人可以帮我吗?

qqswddnsbakn 回答:使用堆栈的顺序遍历的正确性

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/1468777.html

大家都在问