Runtime error for {2,3,1}


  • 0
    L

    Do we have to swap only the values or the whole node, with its left and right has to be swapped ?

    Here's my code.

    class Solution {
    vector<TreeNode *> values;
    public:
    void inorder_walk(TreeNode *root) {
    if(root == NULL)
    return;
    inorder_walk(root->left);
    values.push_back(root);
    inorder_walk(root->right);
    }

    void recoverTree(TreeNode *root) {
        inorder_walk(root);
        int found = -1, swap_var;
        TreeNode **swap; 
        for(int i=0; i<values.size()-1; i++)
        	if (values[i]->val <= values[i+1]->val)
        		continue;
        	else{
        		found++;
        		if(found < 1)
        			swap[0] = values[i];
        		else if(found == 1) {
        			swap_var = swap[0] -> val;
        			swap[0] -> val = swap[1] -> val;
        			swap[1] -> val = swap_var;
        			break;
        		}
        	}
    }
    

    };

    I take the inorder values and see anomolies in it


  • 0
    W

    Do you have global variable?
    Don't use global variable.


Log in to reply
 

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