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


  • 相关阅读:
    20169220 2016-2017-2 <网络攻防实践> 课程总结
    20169220 <网络攻防实践> 第十四周实验—免杀
    20169220 <网络攻防实践> 第十二周实验—SQL注入
    20169220 <网络攻防实践> 第十一周实验—SQL注入+TCP/IP攻击
    20169220 <网络攻防实践> 第十周实验—Nmap+Wireshark+缓冲区溢出漏洞
    20169220 <网络攻防实践> 第九周实验——Nmap
    20169220 <网络攻防实践> 第八周实验——网络攻防虚拟机环境搭建
    20169220 <网络攻防实践> 第七周学习总结
    20169220 <网络攻防实践> 第六周学习总结
    20169220 <网络攻防实践> 第五周学习总结
  • 原文地址:https://www.cnblogs.com/gremount/p/5768005.html
Copyright © 2020-2023  润新知