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;
    }
};

 

posted @ 2018-06-16 14:43  穆穆兔兔  阅读(141)  评论(0)    收藏  举报