Skip to content

Latest commit

 

History

History
22 lines (20 loc) · 606 Bytes

257. Binary Tree Paths.md

File metadata and controls

22 lines (20 loc) · 606 Bytes
class Solution {
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        if (!root) return {};
        vector<string> ret;
        string cur = to_string(root->val);
        dfs(root, cur, ret);
        return ret;
    }
    
    void dfs(TreeNode* root, string cur, vector<string>& ret) {
        if (!root->left && !root->right) {
            ret.push_back(cur);
            return;
        }
        if (root->left) dfs(root->left, cur + "->" + to_string(root->left->val), ret);
        if (root->right) dfs(root->right, cur + "->" + to_string(root->right->val), ret);
    }
};