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

/**
 * 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,int curSum){
        if(!root) return false;
        curSum += root->val;
        if(root->left==NULL && root->right==NULL){
            cout<<curSum<<endl;
            if(curSum == sum) return true;
            return false;
        }
        return hasPathSum(root->left,sum,curSum) || hasPathSum(root->right,sum,curSum);
    }
    bool hasPathSum(TreeNode* root, int sum) {
        return hasPathSum(root,sum,0);
    }
};

Next challenges: (M) Path Sum II (H) Binary Tree Maximum Path Sum (M) Sum Root to Leaf Numbers

Path Sum II

Total Accepted: 65371 Total Submissions: 240118 Difficulty: Medium

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]
]
/**
 * 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 {
private:
    void pathSum(TreeNode* root, vector<vector<int>>& res, vector<int>& oneRes,int sum,int curSum) {
        if(!root) return;
        curSum += root->val;
        oneRes.push_back(root->val);
        if(!root->left && !root->right){
            if(curSum == sum){
                res.push_back(oneRes);
            }
            oneRes.pop_back();
            return;
        }
        pathSum(root->left,res,oneRes,sum,curSum);
        pathSum(root->right,res,oneRes,sum,curSum);
        oneRes.pop_back();
    }
public:
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        vector<vector<int>>  res;
        vector<int> oneRes;
        pathSum(root,res,oneRes,sum,0);
        return res;
    }
};
时间: 2024-11-05 20:39:33

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之“树”:Path Sum &amp;&amp; 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

leetcode第一刷_Combination Sum Combination Sum II

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

os模块 os.stat(&#39;path/filename&#39;) os.path.dirname(path) os.path.exists(path)&#160; os.path.join(path1[, path2[, ...]])

提供对操作系统进行调用的接口 1 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 2 os.chdir("dirname")  改变当前脚本工作目录:相当于shell下cd 3 os.curdir  返回当前目录: ('.') 4 os.pardir  获取当前目录的父目录字符串名:('..') 5 os.makedirs('dirname1/dirname2')    可生成多层递归目录 6 os.removedirs('dirname1')    若

[LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II

Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Pr

os.path.exists(path) 和 os.path.lexists(path) 的区别

使用os.path.exists()方法可以直接判断文件是否存在.代码如下:>>> import os>>> os.path.exists(r'C:\1.TXT')False>>> os.path.exists(path)Return True if path refers to an existing path. Returns False for broken symbolic links. On some platforms, this func

有两个数组a,b,大小都为n,数组元素的值任意整形数,无序,通过交换a,b中的元素,使得|sum(a)-sum(b)|最小

有两个数组a,b,大小都为n,数组元素的值任意整形数,无序: 要求:通过交换a,b中的元素,使数组a元素的和与数组b元素的和之间的差最小. 令A=sum(a)-sum(b) a的第i个元素和b的第j个元素交换后,a和b的和之差为 A'= sum(a) - a[i] + b[j] - (sum(b) - b[j] + a[i])           = sum(a) - sum(b) - 2 (a[i] - b[j])           = A - 2 (a[i] - b[j]) 设x = a[

有两个数组a,b,大小都为n,;通过交换a,b中的元素,使sum(a)-sum(b)最小。

有两个数组a,b,大小都为n,数组元素的值任意整形数,无序: 要求:通过交换a,b中的元素,使数组a元素的和与数组b元素的和之间的差最小. 当前数组a和数组b的和之差为    A = sum(a) - sum(b) a的第i个元素和b的第j个元素交换后,a和b的和之差为    A' = sum(a) - a[i] + b[j] - (sum(b) - b[j] + a[i])           = sum(a) - sum(b) - 2 (a[i] - b[j])           = A

sum 除sum 还是 avg(字段/字段)

create table abc(baduser number(3),playuser number(3))                 ;SELECT t.*,rowid FROM abc t ;select sum(baduser)/sum(playuser),  --1          sum(baduser/playuser)/count(*),  --2           avg(baduser/playuser) --3from    abc; 2和3是结果一样的, 1不同