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

问题描述

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

// 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;
        }
    }}

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...