path-sum leetcode C++
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.
C++
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum2(TreeNode *root, int sum) {
if(NULL == root) return false;
if(NULL == root->left && NULL == root->right && sum == root->val) return true;
if(NULL != root->left && hasPathSum(root->left,sum-root->val)) return true;
if(NULL != root->right && hasPathSum(root->right,sum-root->val)) return true;
return false;
}
bool hasPathSum(TreeNode *root, int sum) {
if(NULL == root) return false;
if(NULL == root->left && NULL == root->right && sum == root->val) return true;
return hasPathSum(root->left,sum-root->val) || hasPathSum(root->right,sum-root->val);
}
};
path-sum leetcode C++的更多相关文章
- 【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 ...
- Path Sum [LeetCode]
Problem Description: http://oj.leetcode.com/problems/path-sum/ Pretty easy. /** * Definition for bin ...
- 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 ...
随机推荐
- js简单化技巧
1.交换两个变量而没有第三个 let x = 1;let y = 2;[x, y] = [y, x];console.log(x, y); 输出: 2 1 2.将数字转换为字符串 const num ...
- C++病毒
病毒代码一#include <bits/stdc++.h> #include <windows.h> using namespace std; void HideWindow( ...
- Java中的三大特性:封装、继承、多态
封装: 概念:封装可以被认为是一个保护屏障,防止该类的代码和数据被其他类随意访问,适当的封装可以让代码更容易理解与维护,也加强了代码的安全性. 原则:将属性隐藏起来,若需要访问某个属性,提供公共方法对 ...
- C博客作业00--顺序分支结构
这个作业属于哪个班级 C语言--网络2011/2012 这个作业的地址 C博客作业00--顺序分支结构 这个作业的目标 初步认识C语法,掌握数据表达.printf.scanf语法及分支结构内容 0.展 ...
- Shell系列(26)- 条件判断之两个文件比较
两个文件之间进行比较 测试选项 作用 文件1 -net 文件2 判断文件1的修改时间是否比文件2的新(如果新则为真) 文件1 -ot 文件2 判断文件1的修改时间是否比文件2的旧(如果旧则为真) 文件 ...
- Linux系列(31) - rpm命令管理之升级与卸载命令(3)
升级命令 rpm -Uvh 包全名(要升级到的软件版本),如果没有安装这个软件的任何版本,这个命令可以代替rpm -ivh. rpm -Uvh 包全名 选项: - -U(upgrade):升级 卸载命 ...
- 一朵云、一张网、一体化 ——GRTN 打造最佳流媒体场景实践
阿里巴巴 GRTN 是面向流媒体云原生设计的,方便客户构建自己的流媒体云原生应用,让流媒体服务无处不在. 在近期召开的分布式云主题报告会上,阿里云资深技术专家卢日发表了题为<GRTN 打造阿里云 ...
- Spring Cloud Gateway 没有链路信息,我 TM 人傻了(上)
本系列是 我TM人傻了 系列第五期[捂脸],往期精彩回顾: 升级到Spring 5.3.x之后,GC次数急剧增加,我TM人傻了 这个大表走索引字段查询的 SQL 怎么就成全扫描了,我TM人傻了 获取异 ...
- DevOps与CICD简介
整体知识点 ·Devops与CI/CD简介 ·Gitlab安装与Git命令使用 ·Gitlab实现持续集成 ·Gitlab实现分支管理 ·Jenkins简介及安装 ·Jenkins插件管理及基础配置 ...
- 发送curl请求的函数
//发送curl请求的函数function curl_request($url, $post = false, $data=array(), $https = false){ //使用curl_ini ...