leetcode 113 Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

Note: A leaf is a node with no children.

Example:

Given the below binary tree and sum = 22,

      5
     / \
    4   8
   /   / \
  11  13  4
 /  \    / \
7    2  5   1

Return:

[
   [5,4,11,2],
   [5,8,4,5]
]

给定一个二叉树和目标值,返回其从根节点到叶节点路径和为目标值的所有路径。本题是102题判断路径和是否存在的升级版,在深度遍历每个左右子节点前,压入当前的节点值,并且深拷贝一份临时结果,然后遍历左右节点,当没有子节点且当前节点值和目标值相等时,符合条件,压入当前节点值,返回true,其他情况返回false。代码记录如下:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        vector<vector<int>> ret;
        vector<int> tmp;
        dfsSum(ret, tmp, root, sum);
        return ret;
    }
    
    bool dfsSum(vector<vector<int>> &ret, vector<int> &tmp, TreeNode *root, int sum)
    {
        if(root == NULL)
            return false;
        if(root->val == sum && root->left == NULL && root->right == NULL)
        {
            //ret.push_back(tmp);
            tmp.push_back(root->val);
            ret.push_back(tmp);
            return true;
        }
        if(root->val != sum && root->left == NULL && root->right == NULL)
            return false;
        tmp.push_back(root->val);
        vector<int> v_left(tmp);
        vector<int> v_right(tmp);
        bool ret_left = dfsSum(ret, v_left, root->left, sum - root->val);
        bool ret_right = dfsSum(ret, v_right, root->right, sum - root->val);
        return ret_left || ret_right;
    }
};

提交后速度只能到50%,对比别人的代码后发现,在数组的深拷贝过程中花费了大量的时间,其实可以在每次判断节点是否满足条件前先尝试压入节点值,然后判断是否满足条件,否的话遍历子节点,遍历完后将刚压入的元素弹出,这样便可省去大量的内存拷贝操作,提高了效率,代码如下dfsSum2函数所示:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        vector<vector<int>> ret;
        vector<int> tmp;
        dfsSum2(ret, tmp, root, sum);
        return ret;
    }
    
    //a lot of memory copy
    bool dfsSum(vector<vector<int>> &ret, vector<int> &tmp, TreeNode *root, int sum)
    {
        if(root == NULL)
            return false;
        if(root->val == sum && root->left == NULL && root->right == NULL)
        {
            //ret.push_back(tmp);
            tmp.push_back(root->val);
            ret.push_back(tmp);
            return true;
        }
        if(root->val != sum && root->left == NULL && root->right == NULL)
            return false;
        tmp.push_back(root->val);
        vector<int> v_left(tmp);
        vector<int> v_right(tmp);
        bool ret_left = dfsSum(ret, v_left, root->left, sum - root->val);
        bool ret_right = dfsSum(ret, v_right, root->right, sum - root->val);
        return ret_left || ret_right;
    }
    
    //use vector as stack
    void dfsSum2(vector<vector<int>> &ret, vector<int> &tmp, TreeNode *root, int sum)
    {
        if(!root)
            return ;
        tmp.push_back(root->val);
        
        if(root->val == sum && root->left == NULL && root->right == NULL)
        {
            ret.push_back(tmp);
        }
        
        dfsSum2(ret, tmp, root->left, sum - root->val);
        dfsSum2(ret, tmp, root->right, sum - root->val);
        tmp.pop_back();
    }

};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值