LeetCode] binaryTreePaths

class Solution {
public:

    void binaryPath(TreeNode* root, vector<string>& result, string path) {
        if (root == NULL) {
            return;
        }
        if (path.empty()) {
            path =  to_string(root->val);
        } else {
            path += "->" + to_string(root-> val);
        }
        if (root->left == NULL && root->right == NULL) {
            result.push_back(path);
            return;
        }
        if (root->left) {
            binaryPath(root->left, result, path);
        }
        if (root->right) {
            binaryPath(root->right, result, path);
        }
    }

    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;
        string path;
        binaryPath(root, result, path);
        return result;
    }
};
原文地址:https://www.cnblogs.com/diegodu/p/9190510.html