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]]
分析: dfs求解即可
/**
* 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:
void find(TreeNode* root, int sum,vector<int>& curPath, vector<vector<int>> & res ){
if(root==nullptr)
return;
if(root->left){
curPath.push_back(root->left->val);
find(root->left, sum-root->val, curPath,res);
curPath.pop_back();
}
if(root->right){
curPath.push_back(root->right->val);
find(root->right, sum-root->val,curPath,res);
curPath.pop_back();
}
if(root->left==nullptr && root->right==nullptr && sum==root->val)
res.push_back(curPath); return;
}
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> res;
if(root==nullptr)
return res;
vector<int> curPath;
curPath.push_back(root->val);
find(root, sum, curPath, res); return res;
}
};
Path Sum II的更多相关文章
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- [leetcode]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 ...
- 【leetcode】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 ...
- 32. Path Sum && Path Sum II
Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if ...
- LeetCode: 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 ...
- [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 p ...
- Path Sum,Path Sum II
Path Sum Total Accepted: 81706 Total Submissions: 269391 Difficulty: Easy Given a binary tree and a ...
- 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 suc ...
- leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III
112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...
随机推荐
- zookeeper集群
0,Zookeeper基本原理 ZooKeeper集群由一组Server节点组成,这一组Server节点中存在一个角色为Leader的节点,其他节点都为Follower.当客户端Client连接到Zo ...
- Source Map调试压缩后代码
在前端开发过程中,无论是样式还是脚本,运行时的文件可能是压缩后的,那这个时候调试起来就很麻烦. 这个时候,可以使用Source Map文件来优化调试,Source Map是一个信息文件,里面储存着原代 ...
- include的用法例子,以及include+merge的用法例子
[include+LinearLayout]的使用例子 AndroidIncludeLayout.java package com.AndroidIncludeLayout; import andro ...
- Android studio 使用Gradle发布Android开源项目到JCenter 总结
1.注册账号 先到https://bintray.com注册一个账号. 这个网站支持 github 账户直接登录的 2.获取 bintray.user 和 bintray.apikey ...
- Android Studio添加aar
1.把aar复制到项目中的 libs 里面 2.在module 里面的build.gradle 的根目录添加 repositories{ flatDir { dirs 'libs' } } 3.在mo ...
- 自动显示隐藏布局的listView
借助View的OnTouchListener接口来监听listView的滑动,通过比较与上次坐标的大小,判断滑动方向,并通过滑动方向来判断是否需显示或者隐藏对应的布局,并且带有动画效果. 1.自动显示 ...
- Java的异常处理
Java的异常处理是通过5个关键字来实现的:try,catch,throw,throws,finally.JB的在线帮助中对这几个关键字是这样解释的: Throws: Lists the ...
- Play Framework 完整实现一个APP(八)
创建Tag标签 1.创建Model @Entity @Table(name = "blog_tag") public class Tag extends Model impleme ...
- Nagios学习实践系列——基本安装篇
开篇介绍 最近由于工作需要,学习研究了一下Nagios的安装.配置.使用,关于Nagios的介绍,可以参考我上篇随笔Nagios学习实践系列——产品介绍篇 实验环境 操作系统:Red Hat Ente ...
- SQL SERVER 属性OWNER不可用于数据库xxx。该对象可能没有此属性,也可能是访问权限不足而无法检索。
今天遇到一个案例:右键单击数据库的属性时出现下面错误提示: 属性Owner不可用于数据库xxx,该对象可能没有此属性,也可能是访问权限不足而无法检索. 使用脚本查看该数据库的Owner时发现Owner ...