二叉树运行时错误的后置迭代遍历

时间:2018-10-22 04:00:58

标签: c++ iteration traversal postorder

我正在做一些LeetCode问题(LeetCode中的新问题),并且我写了一个解决方案,用于迭代遍历二叉树。我使用了堆栈,我相信我的逻辑可以工作,但是LeetCode给了我一个运行时错误。我怎样才能解决这个问题?

这是我的代码:

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        TreeNode* temp = root; 
        vector<int> v; 
        stack<TreeNode*> s; 

        if (temp == NULL)
            return v; 
        while (true){
            while (temp != NULL){
                if (temp->right)
                    s.push(temp->right); 
                s.push(temp); 
                temp = temp->left; 

            }
            if (s.empty())
                break; 
            temp = s.top();
            s.pop(); 
            if (s.top() == temp->right){
                s.pop(); 
                s.push(temp); 
                temp = temp->right;                 

            }else{
                v.push_back(temp->val); 
                temp = NULL; 
            }
        }
        return v; 

    }
};

请帮助,谢谢!

2 个答案:

答案 0 :(得分:1)

当堆栈中只剩下一项时,您的代码将在此处崩溃:

temp = s.top();               // remove the last item from the stack
s.pop();                      // empty the stack
if (s.top() == temp->right){  // attempt to access the top of the stack.. boom!

解决方法是在检查top之前测试堆栈是否为空:

if (!s.empty() && s.top() == temp->right) {

答案 1 :(得分:1)

更正的代码:        其他检查是否要在堆栈的空缺处添加检查

    #include<iostream>
    #include<vector>
    #include<stack>
    using namespace std;
     class TreeNode{public:
     int val;
     TreeNode *left;
     TreeNode *right;
     TreeNode(){
      left=right=NULL;
           }
       TreeNode(int data){
          val=data;
              }
         };
       class Solution {
        public:
      vector<int> postorderTraversal(TreeNode* root) 
             {
           TreeNode* temp = root; 
            vector<int> v; 
           stack<TreeNode*> s; 

           if (temp == NULL)
            return v; 
           while (true){
           while (temp != NULL){
            if (temp->right)
                s.push(temp->right); 
            s.push(temp); 
            temp = temp->left; 

        }
        if (s.empty())
            break; 
        temp = s.top();
        s.pop(); 
        if (!s.empty() && s.top() == temp->right) {
            s.pop(); 
            s.push(temp); 
            temp = temp->right;                 

        }else{
            v.push_back(temp->val); 
            temp = NULL; 
        }
    }
    return v; 

    }
      };
       int main(){
       TreeNode* root = NULL; 
        root = new TreeNode(1); 
        root->left = new TreeNode(2); 
         root->right = new TreeNode(3); 
       root->left->left = new TreeNode(4); 
       root->left->right = new TreeNode(5); 
         root->right->left = new TreeNode(6); 
         root->right->right = new TreeNode(7); 
        Solution obj;
         vector<int >v;
        v =obj.postorderTraversal(root);
        for(auto i=v.begin();i!=v.end();++i)
        cout<<*i;

    }

输出: 4526731