Why this cause an RE?


  • 1
    V

    I really can't figure out why

    test case input [1,2] won't pass

    and i think there should be no dereference nullptr

    class Solution {
    public:
        void flatten(TreeNode* root) {
            TreeNode* T = root;
            if(T == nullptr) return;
            unvist.push(T);
            TreeNode* PreT = nullptr;
            while(!unvist.empty()){
                T = unvist.top();
                unvist.pop();
                if(PreT != nullptr) PreT->right = T;
                if(T->right != nullptr) unvist.push(T->right);
                if(T->left != nullptr) unvist.push(T->left);
                PreT = T;
            }
        }
    private:
        stack<TreeNode*> unvist;
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.