zoukankan      html  css  js  c++  java
  • LeetCode:Path Sum I II

    LeetCode:Path Sum

    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 /**
     2  * Definition for binary tree
     3  * struct TreeNode {
     4  * int val;
     5  * TreeNode *left;
     6  * TreeNode *right;
     7  * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     bool hasPathSum(TreeNode *root, int sum) {
    13         // IMPORTANT: Please reset any member data you declared, as
    14         // the same Solution instance will be reused for each test case.
    15         if(root == NULL)return false;
    16         if(root->left == NULL && root->right == NULL && root->val == sum)
    17             return true;
    18         if(root->left && hasPathSum(root->left, sum - root->val))
    19             return true;
    20         if(root->right && hasPathSum(root->right, sum - root->val))
    21             return true;
    22         return false;
    23     }
    24 };

    LeetCode: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.

    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]
    ]                                                                       本文地址

    同理也是递归求解,只是要保存当前的结果,并且每次递归出来后要恢复递归前的结果,每当递归到叶子节点时就把当前结果保存下来。

     1 /**
     2  * Definition for binary tree
     3  * struct TreeNode {
     4  * int val;
     5  * TreeNode *left;
     6  * TreeNode *right;
     7  * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     vector<vector<int> > pathSum(TreeNode *root, int sum) {
    13         // IMPORTANT: Please reset any member data you declared, as
    14         // the same Solution instance will be reused for each test case.
    15         vector<vector<int> > res;
    16         if(root == NULL)return res;
    17         vector<int> curres;
    18         curres.push_back(root->val);
    19         pathSumRecur(root, sum, curres, res);
    20         return res;
    21     }
    22     void pathSumRecur(TreeNode *root, int sum, vector<int> &curres, vector<vector<int> >&res)
    23     {
    24         if(root->left == NULL && root->right == NULL && root->val == sum)
    25         {
    26             res.push_back(curres);
    27             return;
    28         }
    29         if(root->left)
    30         {
    31             curres.push_back(root->left->val);
    32             pathSumRecur(root->left, sum - root->val, curres, res);
    33             curres.pop_back();
    34         }
    35         if(root->right)
    36         {
    37             curres.push_back(root->right->val);
    38             pathSumRecur(root->right, sum - root->val, curres, res);
    39             curres.pop_back();
    40         }
    41     }
    42 };

    【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3440044.html

  • 相关阅读:
    看看自己敲了多少代码
    jquery中获取单选标签redio的val
    @functools.wrapes
    SQLAlachemy 自动提交配置 SQLALCHEMY_COMMIT_ON_TEARDOWN
    ajax 常用格式
    开发文档收藏
    falsk 请求钩子
    数组中出现次数超过数组长度一半的值 分类: C/C++ 2015-07-09 15:38 142人阅读 评论(0) 收藏
    二叉搜索树(C++) 分类: C/C++ 数据结构与算法 2015-07-09 11:18 205人阅读 评论(0) 收藏
    程序员必读的六本书 2015-07-08 11:26 21人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/TenosDoIt/p/3440044.html
Copyright © 2011-2022 走看看