<LeetCode OJ> 112/113. Path Sum(I / II)

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.



分析:

写在看别人答案之前:

1,不知道如何判断不是pathsum的情况,因为无法根据某一条不是就返回fasle。如果是倒数好判断

以下是错误答案,只通过了69个案例,有朝一日再来看

/**
 * 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 getDfsSums (TreeNode* node, int pathsum) {
            if(!node->left && !node->right )    
             {
                if(pathsum==dstsum)  
                    return true;
                else
                    return false;
             }
            if(node->left)  
                getDfsSums(node->left, pathsum+node->val);  
            if(node->right)  
                getDfsSums(node->right, pathsum+node->val);
        } 
    bool hasPathSum(TreeNode* root, int sum) {
        if(!root)   
            return false;
        dstsum=sum;
        return getDfsSums(root, root->val);  
    }
    int dstsum;
};


别人的答案

原来用或运算来判断有true的路劲

/**
 * 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) {
        if(!root)
            return false;
        if(!root->left && !root->right ){
            if(root->val==sum)
                return true;
            else
                return false;
        }
        return hasPathSum(root->left, sum-(root->val)) || hasPathSum(root->right, sum-(root->val));
    }
};


学习和改写别人的答案:

/**
 * 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 IsPathSum (TreeNode* node, int pathsum) {  //此时的pathsum并未机上当前node的节点值(如果不为NULL的话)
        if(!node)  
            return false;  
        if(!node->left && !node->right ){  
            if(pathsum+node->val==dstsum)  //如果加上当前节点就是sum,结果已出
                return true;  
            else  
                return false;  
        }           
        return IsPathSum(node->left, pathsum+node->val)||IsPathSum(node->right, pathsum+node->val); //直接在函数中加上当前节点的值 
    }   
    bool hasPathSum(TreeNode* root, int sum) {
        if(!root)  
            return false;  
        dstsum=sum;    
        return IsPathSum(root,0);  
    }
    int dstsum;
};




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



分析:
深度优先搜索,利用递归保存中间的计算结果,递归到叶子即刻判断和是否为目标sum

/**
 * 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 getDfsSums(vector<int> ans, TreeNode* node, int pathsum) {  
            ans.push_back(node->val);
            pathsum+=node->val;
            
            if(!node->left && !node->right)  {    
                if(pathsum==dstsum) result.push_back(ans);
                return;  
            }
           
            if(node->left)  
                getDfsSums(ans, node->left, pathsum);  
            if(node->right)  
                getDfsSums(ans, node->right, pathsum);
        } 
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        if(!root)   
            return result;
        vector<int> ans;
        dstsum=sum;
        getDfsSums(ans, root, 0);  
        return result;     
    }
    int dstsum;
    vector< vector<int> > result;  
};


注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50495606

原作者博客:http://blog.csdn.net/ebowtang

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值