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 };
时间: 2024-10-16 23:14:22

LeetCode之“树”:Path Sum && Path Sum II的相关文章

32. Path Sum &amp;&amp; Path Sum II

Path Sum OJ: https://oj.leetcode.com/problems/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

[Leetcode][Tree][Binary Tree Maximum Path Sum]

找书中权值和最大的路径,至少包含一个点. 有点类似LCA(最近公共祖先),树的问题关键是如何划分子问题,然后递归求解. 想到了可以返回两种结果,一个是单独路径的最大和,一种是子树的最大和,然后在求解的过程中不断的更新答案. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val

【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 t

Path Sum,Path Sum II

Path Sum Total Accepted: 81706 Total Submissions: 269391 Difficulty: Easy 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

【Leetcode】【Easy】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 t

【leetcode】Binary Tree Maximum Path Sum

Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1 / 2 3 Return 6. 用递归确定每一个节点作为root时,从root出发的最长的路径 在每一次递归中计算maxPath 1 /** 2 * Def

leetcode第一刷_Combination Sum Combination Sum II

啊啊啊啊,好怀念这种用递归保存路径然后打印出来的题目啊,好久没遇到了. 分了两种,一种是可以重复使用数组中数字的,一种是每个数字只能用一次的.其实没有多大区别,第一种每次进入递归的时候都要从头开始尝试,第二种要找一个标记的数组,把已经用到过的排除掉,就像生成全排列时的做法一样.跟我一样用引用保存中间结果的话,要注意回退的情况.第二种回退时,要把用到的那个数也恢复为可用,就完全像全排列时做的一样.破例贴两个题的代码,因为他们是在是不值得用两片文章来写. class Solution { publi

LeetCode OJ - Binary Tree Maximum Path

这道题需要注意的地方有以下一些: 1. 求从子树中的某节点到当前节点的最大路径不能采用递归方法,因为这个部分会被反复的调用,如果用递归,会使得之前已经计算过的节点被重复计算,使得时间复杂度特别高: 2. 树中有节点的值是负数的. 下面是AC代码.(我发现AC并不代表代码真的完全正确!!) 1 /** 2 * Given a binary tree, find the maximum path sum. 3 * The path may start and end at any node in t

Leetcode刷题录之Two Sum

题意大概是给出一个数列num,和一个目标数target,然后要找出数列中的两个数,使得这两个数之和等于目标数,输出这两个数的下标值(从1开始算). 一个比较暴力的方法是用一个二重循环直接遍历序列,在第一重循环中找到a,在第二重循环中找到b,使得a+b=target,这种做法的时间复杂度是O(n^2), 提交时提示超时. 改进方法,先对数列num复制一个副本,然后对副本进行排序.在一重循环中找到a,接着对这个有序的副本进行二分查找,找到b= target-a,二分查找的 时间复杂度是O(logn)