Leetcode::Pathsum & Pathsum II

Pathsum

Description:

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.

分析:二叉树root-to-leaf的查找,深搜搜到结果返回即可


 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 int sumval = root->val;
15 if(sumval==sum && root->left==NULL &&root->right==NULL) return true;
16 else{
17 return (hasPathSum(root->left,sum-sumval)||hasPathSum(root->right,sum-sumval));
18 }
19 }
20 };

PathsumII

Description:

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]
]
分析:这一题和上一题的区别在于不仅需要判断有没有,还需要记录是什么。在深搜的时候维护一个stack(这里用vector实现),记录已经走过的路径,
返回是栈也弹出相应节点


 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 pathrec(vector<vector<int> > &rec, vector<int>& path,TreeNode* r,int sum)
13 {
14 sum-=r->val;
15 //if(sum<0) return false;
16 //Tell if it‘s a leaf node
17 if(r->left ==NULL && r->right == NULL)
18 {
19 if(sum!=0) return false;
20 else{
21 vector<int> one = path;
22 one.push_back(r->val);
23 rec.push_back(one);
24 return true;
25 }
26 }
27 //Ordinary node
28 path.push_back(r->val);
29 bool flag = false;
30 if(r->left!=NULL) pathrec(rec,path,r->left,sum);
31 if(r->right!=NULL) pathrec(rec,path,r->right,sum);
32 path.erase(path.end()-1);
33 return flag;
34
35 }
36 vector<vector<int> > pathSum(TreeNode *root, int sum) {
37 vector<vector<int> > rec;
38 if(root==NULL) return rec;
39 vector<int> path;
40 pathrec(rec,path,root,sum);
41
42 return rec;
43 }
44 };

 

Leetcode::Pathsum & Pathsum II,布布扣,bubuko.com

时间: 2024-10-21 21:01:28

Leetcode::Pathsum & Pathsum 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: Path Sum II [113]

[题目] 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 --- 90. Subsets II

题目链接:Subsets II Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2]

【leetcode】N-queens II

问题: 返回N皇后问题解的个数. 分析: 详见 N-queens 实现: bool nextPermutation(vector<int> &num) { int i = num.size() - 1; while (i >= 1) { if(num[i] > num[i - 1]) { --i; int ii = num.size() - 1; while (ii > i && num[ii] <= num[i]) --ii; if(ii &g

leetcode: Subsets &amp; Subsets II

SubsetsGiven a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2

[LeetCode] Jump Game II(贪婪算法)

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

leetCode: Single Number II [137]

[题目] Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他

LeetCode: Jump Game II [044]

Perface 如果让你实现这个页面和一些操作的,比如点击1.2.3等就在那个input text中显示,还有删除功能,拨打我们先不要管它,只是模拟而已.要是我刚开始做的话,我会这样做: 用css.HTML布局那个界面 用javascript的事件委托监听那个按钮的父节点的点击事件 但是如果我想用面向对象的思想做呢?我是用Ext做的,所以我想说的是它帮我封装了很多.可能一些没用过Ext的人不太了解我下面贴的代码,但是我会尽量解释清楚的! Description ContactTelPanel =

LeetCode: Combination Sum II [039]

[题目] Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be