【问题标题】:segmentation fault while doing iterative inorder tree traversal进行迭代中序树遍历时出现分段错误
【发布时间】:2019-12-19 23:57:16
【问题描述】:

我正在使用堆栈对二叉树进行迭代搜索。但是我遇到了分段错误。我已经交叉检查了很多次,但找不到任何东西。请帮忙。

void inOrder(struct Node *root) 
{ 
    stack<Node *> s; 
    Node *t;
    s.push(root);

    while (s.empty() == false) { 
        t = s.top();
        while (t->left != NULL) {
            s.push(t->left);
            t = t->left;
        }
        while (1) {
            t = s.top();
            s.pop();
            cout << t->data << " ";
            if (t->right != NULL) {
                s.push(t->right);
                break;
            }
        }
    } /* end of while */
}

【问题讨论】:

    标签: pointers data-structures segmentation-fault binary-tree


    【解决方案1】:

    while(1){

    应该是:

    while (!s.empty()) {
    

    【讨论】:

      猜你喜欢
      • 2021-11-27
      • 1970-01-01
      • 2013-08-22
      • 2021-10-30
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多