zoukankan      html  css  js  c++  java
  • 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 };
  • 相关阅读:
    HTML5/CSS3速成教程
    ECMAScript5.1
    HTML5新特性有哪些,你都知道吗
    如何写出兼容性很好的页面
    数据库三个范式详解
    UML入门
    前端总结·基础篇·CSS(一)布局
    常用的14种HTTP状态码速查手册
    传输层协议TCP和UDP
    js获取地址栏参数
  • 原文地址:https://www.cnblogs.com/xiehongfeng100/p/4629988.html
Copyright © 2011-2022 走看看