首页 > 其他 > 详细

剑指offer 二叉树中和为某一值得路径

时间:2018-09-02 20:49:30      阅读:199      评论:0      收藏:0      [点我收藏+]
class Solution {
public:
    void recur(TreeNode* root, const int expectNumber, int curr, vector<vector<int>>& res, vector<int>& path){
        path.push_back(root->val);
        curr += root->val;
        bool isLeaf = (root->left == nullptr && root->right == nullptr);
        if(isLeaf && curr == expectNumber){
            res.push_back(path);
        }
        if(root->left != nullptr){
            recur(root->left, expectNumber, curr, res, path);
        }
        if(root->right != nullptr){
            recur(root->right, expectNumber, curr, res, path);
        }
        path.pop_back();
    }

    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
        vector<vector<int>> res;
        vector<int> path;
        if(root == nullptr) return res;
        int curr = 0;
        recur(root, expectNumber, curr, res, path);
        return res;
    }
};

剑指offer 二叉树中和为某一值得路径

原文:https://www.cnblogs.com/theodoric008/p/9575202.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!