I got AC without a return in main function (c++)


  • 0
    A

    I would really appreciate it if some one can tell me why. I am so confused. I tried to declare the return vector every possible place with any name and got AC all the time without returning it.

    (I tried for more than 20 times and all got AC. PS, with a return I got AC as expected.)

    class Solution {
    public:
        vector<TreeNode*> generateTrees(int end, int begin = 1){
            TreeNode * root;
            vector<TreeNode*> leftchildren,rightchildren;
            vector<TreeNode*> result;
            
            if(begin > end) 
            {
                result.push_back(NULL);
                return result;
            }
    
            for(int i = begin; i <= end; i++)
            {
                leftchildren = generateTrees(i - 1, begin);
                rightchildren = generateTrees(end, i+1);
                for(auto l: leftchildren)
                    for(auto r:rightchildren)
                    {
                        root = new TreeNode(i);
                        root->left = l;
                        root->right = r;
                        result.push_back(root);
                    }
            }
    
            //return result; //where it suppose to be
        }
    };

  • 0
    Y

    In assembly, eax is used as the return value. The reason is just that at the end of your function, eax is set as 'result' by coincidence.


  • 0
    A

    @y2m3 Haha, this is hilarious.


Log in to reply
 

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