• LeetCode之“树”:Path Sum && Path Sum II


    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.

      这道题采用深度优先搜索的方法就可以了,具体程序(12ms)如下:

     1 /**
     2  * Definition for a binary tree node.
     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         if(!root)
    14             return false;
    15         return hasPathSumSub(root, 0, sum);
    16     }
    17     
    18     bool hasPathSumSub(TreeNode *tree, int subSum, int sum)
    19     {
    20         if(!tree)
    21             return false;
    22             
    23         subSum += tree->val;
    24         if(!tree->left && !tree->right && subSum == sum)
    25             return true;
    26         
    27         return hasPathSumSub(tree->left, subSum, sum) || hasPathSumSub(tree->right, subSum, sum);
    28     }
    29 };

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

      这题与上题类似。具体程序(24ms)如下:

     1 /**
     2  * Definition for a binary tree node.
     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         vector<vector<int>> rVec;
    14         if(!root)
    15             return rVec;
    16         
    17         vector<int> vec;
    18         hasPathSumSub(root, vec, rVec, 0, sum);
    19         return rVec;
    20     }
    21     
    22     void hasPathSumSub(TreeNode *tree, vector<int> vec, vector<vector<int>>& rVec, int subSum, int sum)
    23     {
    24         if(!tree)
    25             return;
    26         
    27         vec.push_back(tree->val);
    28         subSum += tree->val;
    29         if(!tree->left && !tree->right && subSum == sum)
    30             rVec.push_back(vec);
    31         
    32         hasPathSumSub(tree->left, vec, rVec, subSum, sum);
    33         hasPathSumSub(tree->right, vec, rVec, subSum, sum);
    34     }
    35 };
  • 相关阅读:
    vmware克隆linux出错:Device eth0 does not seem to be present
    IDEA中项目路径问题
    Intellij idea 报错:Error : java 不支持发行版本5
    equals和==的区别
    指定Python版本下pip安装“pip install”
    大数据5V特点(5Vs of Big Data)
    大二下每周总结
    大二 下学期——期末个人总结(课程评价及加分项)
    大二下学期第二次个人作业第二阶段
    大二下学期第二次个人作业第二阶段
  • 原文地址:https://www.cnblogs.com/xiehongfeng100/p/4629988.html
Copyright © 2020-2023  润新知