Why my code get an wrong result


  • 0
    A

    class Solution {
    public:
    vector<int> preorderTraversal(TreeNode *root) {
    vector<int> ret;
    if(root==NULL) return ret;
    else
    {

            ret.push_back(root->val);
            preorderTraversal(root->left);
            preorderTraversal(root->right);
        }
    }
    

    };


  • 0
    T

    You should declare the data type for vector, like vector< int >, change vector ret like:
    vector< int > ret


  • 0
    S

    It's wrong because you don't use the return values from the recursive calls.


Log in to reply
 

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