Leetcode题 112 和 113. Path Sum I and II

112题目如下:

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:
Given the below binary tree and  sum = 22 ,
              5
             / \
            4   8
           /   / \
          11  13  4
         /  \      \
        7    2      1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

113题目如下:

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

For 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]
]

112和113题目是类似的,都是找出等于给定值的路径,不过前者只看有没有,后者是要输出所有符合条件的路径。

112由于只要看有没有等于给定值的路径,所以可以用BFS,将每个树节点的val改为从根节点到当前节点的距离,这样改到树的叶子节点的时候,就可以判断是否有叶子节点的距离值符合要求。

113由于需要输出所有符合条件的路径,如果树节点的数据结构不可以改,那这就不适合用BFS,因为如果用BFS需要在每个树节点里维护一个到达当前节点经过的节点记录;所以这里改用DFS,通过修改一个存储路径的vector来统计所有满足条件的路径。

112题目代码

/**
 * 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:
    bool hasPathSum(TreeNode* root, int sum) {
        queue<TreeNode*> q;
        vector<TreeNode*> v;
        if(root==NULL) return false;
        q.push(root);
        TreeNode* p=root;
        while(q.size()!=0)
        {
            p=q.front();
            q.pop();
            if(p->left!=NULL)
            {
                p->left->val+=p->val;
                q.push(p->left);
            }
            if(p->right!=NULL)
            {
                p->right->val+=p->val;
                q.push(p->right);
            }
            if(p->left==NULL && p->right==NULL)
                v.push_back(p);
        }
        for(int i=0;i<v.size();i++)
            if(v[i]->val==sum) return true;
        return false;
        
    }
};

113题目代码

/**
 * 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:
    //用来递归的函数
    void findSum(TreeNode* t,vector<int>& temp, vector<vector<int> >& record, int sum, int cur)
    {
    	temp.push_back(t->val);
    	cur+=t->val;
    	//在叶节点找到了目标值
    	if(cur==sum && t->left==NULL && t->right==NULL) {
    		record.push_back(temp);
    		temp.erase(temp.end()-1);
    		cur-=t->val;
    		return ;
    	}
    	//查找左右节点
    	if(t->left!=NULL) findSum(t->left, temp, record, sum , cur);
    	if(t->right!=NULL) findSum(t->right, temp, record, sum , cur);
    	//返回前更新临时路径的记录和累加和
    	temp.erase(temp.end()-1);
    	cur-=temp[temp.size()-1];
    	return;
    }
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        vector<vector<int> > record;//记录最终要返回的路径
        if(root==NULL) return record;
	    vector<int> temp;//记录当前临时路径
	    findSum(root,temp,record,sum,0);
	    return record;
    }
};


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值