What's the problem with this ? for input {1,2} output is [1,1,2] instead of [1,2].


  • 0
    P
    vector<int> result;
    void helpfunction(TreeNode *x){
     if(x==NULL)
     return ;
     result.push_back(x->val);
     helpfunction(x->left);
     helpfunction(x->right);
    

    }

    class Solution {

    public:

    vector<int> preorderTraversal(TreeNode *root) {
        helpfunction(root);
        return result;
    }
    

    };


Log in to reply
 

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