C++ Solution


  • 0
    S

    class Solution {
    public:
    vector<string> a;
    vector<string> binaryTreePaths(TreeNode* root) {
    if(root==NULL){
    return a;
    }
    if(!root->left && !root->right){
    a.push_back(to_string(root->val));
    return a;
    }
    if(root->left)
    get_path(root->left, to_string(root->val));
    if(root->right)
    get_path(root->right, to_string(root->val));
    return a;
    }
    void get_path(TreeNode* root, string prefix){
    if(!root->left && !root->right)
    a.push_back(prefix + "->" + to_string(root->val));
    if(root->left){
    get_path(root->left, prefix + "->" + to_string(root->val));
    }
    if(root->right){
    get_path(root->right, prefix + "->" + to_string(root->val));
    }
    }
    };


Log in to reply
 

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