What's wrong with my code? continue to pop out runtime error


  • 0
    G
    class Solution {
    public:
        TreeNode *first;
        TreeNode *second;
        void travel(TreeNode *root,TreeNode *pre){
            if(root==NULL)   
                return;
            else{
                travel(root->left,pre);
                if(pre==NULL){
                    pre=root;
                }else{
                    if(pre->val>root->val){
                        if(first==NULL)
                            first=pre;
                        second=root;
                    }
                    pre=root;
                }
                travel(root->right,pre);
            }
        }
        void recoverTree(TreeNode *root) {
            TreeNode *pre=NULL;
            first=NULL;
            second=NULL;
            travel(root,pre);
            int value=first->val;
            first->val=second->val;
            second->val=value;
        }
    };

Log in to reply
 

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