Path SumI、II——给出一个数,从根到子的和等于它

I、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 andsum = 22,

              5
             /             4   8
           /   /           11  13  4
         /  \              7    2      1

return true, as there exist a root-to-leaf path5->4->11->2which sum is 22.

PS:要想好判断条件,递归的时候考虑到必须要到子节点。

 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         if(root==NULL) return false;
14         return dfs(root,sum);
15     }
16     bool dfs(TreeNode *root, int sum){
17         if(root==NULL) return false;
18         if(root->left==NULL&&root->right==NULL&&sum==root->val) return true;
19         return dfs(root->left,sum-root->val)||dfs(root->right,sum-root->val);
20     }
21 };

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 andsum = 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         dfs(root,sum);
14         return res;
15     }
16     void dfs(TreeNode *root, int sum){
17         if(root==NULL) return;
18         v.push_back(root->val);
19         if(root->left==NULL&&root->right==NULL&&root->val==sum){
20             res.push_back(v);
21         }else{
22             dfs(root->left,sum-root->val);
23             dfs(root->right,sum-root->val);
24         }
25         v.pop_back();
26     }
27     vector<int> v;
28     vector<vector<int>> res;
29 };
时间: 2024-08-27 19:31:23

Path SumI、II——给出一个数,从根到子的和等于它的相关文章

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] ] 给定一个二叉树和一个值,找出所有根到叶的路径和等于

[LeetCode][JavaScript]Path Sum II

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] ] https://leetco

Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5646   Accepted: 1226 Description In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of edges on p: ⊕ is the xor operator. We say a path the xor-l

【LeetCode】113. Path Sum II 基于Java和C++的解法及分析

113. Path Sum II Total Accepted: 80509 Total Submissions: 284188 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

[Leetcode Week14]Path Sum II

Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. Example Given the below binary tree and sum = 22 5

IT公司100题-6-根据上排给出十个数,在其下排填出对应的十个数

问题描述: 给你10分钟时间,根据上排给出十个数,在其下排填出对应的十个数要求下排每个数都是先前上排那十个数在下排出现的次数.上排的十个数如下:[0,1,2,3,4,5,6,7,8,9] 举一个例子,数值: 0,1,2,3,4,5,6,7,8,9分配: 6,2,1,0,0,0,1,0,0,00在下排出现了6次,1在下排出现了2次,2在下排出现了1次,3在下排出现了0次-.以此类推.. 问题分析: 它的原型跟八皇后有点类似,都是用回溯递归的方法去一次一次尝试,直到找出正确解. 具体的想法是:不断的

javascript基础程序(算出一个数的平方值、算出一个数的阶乘、输出!- !- !- !- !- -! -! -! -! -! 、函数三个数中的最大数)

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script> /* 算出一个数的平方值 function add(a){ var b=Math.sqrt(a); return b; } alert(add(3));*/ /*// 算出一个数的阶乘 func

PAT (Basic Level) Practise 1002. 写出这个数

1002. 写出这个数 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式:在一行内输出n的各位数字之和的每一位,拼音数字间有1 空格,但一行中最后一个拼音数字后没有空格. 输入样例: 1234567890987654321123456789 输出样

1002. 写出这个数

1 /* 2 * Main.c 3 * 1002. 写出这个数 4 * Created on: 2014年6月14日 5 *****测试通过********* 6 */ 7 8 #include <stdio.h> 9 #include <string.h> 10 11 int main(void){ 12 13 char num[1001]; 14 int temp[10]; 15 unsigned int sum=0; 16 char result[10][5]={ 17 {&