Path Sum [LeetCode]
Problem Description: http://oj.leetcode.com/problems/path-sum/
Pretty easy.
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> getSums(TreeNode *root) {
vector<int> sums; if(root->left == NULL && root->right == NULL)
sums.push_back(root->val); if(root->left != NULL){
vector<int> left_sums;
left_sums = getSums(root->left);
for(auto item : left_sums) {
sums.push_back(item + root->val);
}
} if(root -> right != NULL) {
vector<int> tmp_sums;
tmp_sums = getSums(root->right);
for(auto item : tmp_sums) {
sums.push_back(item + root->val);
}
} return sums;
} bool hasPathSum(TreeNode *root, int sum) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(root == NULL)
return false; vector<int> sums = getSums(root); for(auto item : sums) {
if(item == sum)
return true;
} return false;
}
};
Path Sum [LeetCode]的更多相关文章
- 【LeetCode】Path Sum ---------LeetCode java 小结
Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- Binary Tree Maximum Path Sum leetcode java
题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...
- Binary Tree Maximum Path Sum - LeetCode
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- Minimum Path Sum [LeetCode]
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- Minimum Path Sum——LeetCode
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- Minimum Path Sum leetcode java
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...
- Path Sum leetcode java
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- Binary Tree Maximum Path Sum [leetcode] dp
a(i):在节点i由于单边路径的最大结束 b(i):在节点i路径和 a(i) = max{ i->val, i->val + max{a(i->left), a(i->righ ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
随机推荐
- System.MissingMethodException: 找不到方法:
This is a problem which can occur when there is an old version of a DLL still lingering somewhere ar ...
- Linux系统下如何配置SSH?如何开启SSH
查询\安装SSH服务 1.登陆linux系统,打开终端命令.输入 rpm -qa |grep ssh 查找当前系统是否已经安装 2.如果没有安装SSH软件包,可以通过yum 或rpm安装包进行安装( ...
- Codeforces Round #257 (Div. 2) A题
A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 关于seafile启动的问题解决
过了一个国庆,同事反映说seafile服务挂掉了,无法正常连接. 刚才解决了一下,把相关问题简要记录一下: 1.首先连接阿里云,获得相关IP地址(如果已知IP地址,则不需要该步骤)
- 保存会话数据——cookie学习
Cookie是客户端技术,程序把每个用户的数据以cookie的形式写给用户各自的浏览器.当用户使用浏览器再去访问服务器中的web资源时,就会带着各自的数据去.这样,web资源处理的就是用户各自的数据了 ...
- MyEclipse8.5启动无法选择工作空间的问题
方法一:打开Window---Preferences---General---Startup and Shutdown,勾选Prompt for workspace on startup 选项,再次登 ...
- u盘在电脑读不出来,但别的可以读,别的u盘在我电脑又可以识别怎么回事?
不知道我的U盘是怎么回事,在我自己的电脑里读不出来,下面有U盘图标,但我的电脑里就是找不到U盘盘符,但把这个U盘放其他电脑上又可以读取,我以为是我的电脑的问题,但用其他的U盘插我电脑又没问题,完全摸不 ...
- Vim优化
写python代码时,希望缩进是4个空格,而不是制表符tab, 在vim中,我们只需要简单配置一下就ok了,打开~/.vimrc加上下面的几行(如果已经有了,修改一下数值就行了). set tabst ...
- ip变更导致连接不到mysql的解决办法
第一步:ssh连接到服务器 第二步:连接mysql mysql -u root -psqj888 第三步:切换到mysql数据库 use mysql 第四步:查询mysql的user表 SELECT ...
- html frames
http://blog.sina.com.cn/s/blog_67697189010116o0.html *********************************************** ...