Why there exist ["1->2->0"] when run the code on OJ?


  • -1
    W

    vector<string> binaryTreePaths(TreeNode* root) {
    vector<string> ret;
    if(!root) return ret;
    findPaths(ret, root, to_string(root->val));
    return ret;
    }
    void findPaths(vector<string>& vec, TreeNode* root, string s){
    if(!root->left && !root->right)
    {
    vec.push_back(s);
    return;
    }
    if(root->left)
    findPaths(vec, root->left, s + "->" + to_string(root->left->val));
    if(root->right)
    findPaths(vec, root->right, s + "->" + to_string(root->right->val));
    }


Log in to reply
 

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