LeetCode 112. Path Sum(路径和是否可为sum)
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.
Note: A leaf is a node with no children.
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.
/**
* 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:
bool hasPathSum(TreeNode* root, int sum) {
if (root == NULL) return false;
if (root->val == sum && root->left == NULL && root->right == NULL)
{
return true;
} return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); }
};
LeetCode 112. Path Sum(路径和是否可为sum)的更多相关文章
- [LeetCode] 112. Path Sum 二叉树的路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)
Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...
- [LeetCode] 112. Path Sum 路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- LeetCode 112. Path Sum (二叉树路径之和)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- LeetCode 112 Path Sum(路径和)(BT、DP)(*)
翻译 给定一个二叉树root和一个和sum, 决定这个树是否存在一条从根到叶子的路径使得沿路全部节点的和等于给定的sum. 比如: 给定例如以下二叉树和sum=22. 5 / \ 4 8 / / \ ...
- 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 ...
- Java [Leetcode 112]Path Sum
题目描述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...
- [leetcode] #112 Path Sum (easy)
原题链接 题意: 给定一个值,求出从树顶到某个叶(没有子节点)有没有一条路径等于该值. 思路: DFS Runtime: 4 ms, faster than 100.00% of C++ class ...
- LeetCode 112. Path Sum 二叉树的路径和 C++
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [leetcode]112. Path Sum路径和(是否有路径)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- Windows系统下安装VirtualBox及安装Ubuntu16.04
1.软件介绍 VirtualBox VirtualBox 是一款免费的开源虚拟机软件,所谓虚拟机软件,就是能够提供各种模拟的硬件环境,并且在其上安装各种操作系统,目前支持Window,Linux,Ma ...
- zabbix--图形字体乱码
解决 zabbix 图形字体乱码 如图:修改之前 具体步骤: 1)下载字体,例如:SIMKAI.ttf楷体(也可直接将Windows上的直接上传:Windows路径:C:\Windows\Fonts) ...
- mysql创建临时表,将查询结果插入已有的表
A.临时表再断开于mysql的连接后系统会自动删除临时表中的数据,但是这只限于用下面语句建立的表:1)定义字段 CREATE TEMPORARY TABLE tmp_table ( nam ...
- XML炸弹
XML炸弹XML document type definition (DTD)可以定义entity,DTD可以出现在外部文件或文件内部.利用DTD可以产生XML炸弹,也就是能迅速占用大量内存的文件,如 ...
- web渗透—xss攻击如何防御
1.基于特征的防御 XSS漏洞和著名的SQL注入漏洞一样,都是利用了Web页面的编写不完善,所以每一个漏洞所利用和针对的弱点都不尽相同.这就给XSS漏洞防御带来了困难:不可能以单一特征来概括所有XSS ...
- Laravel5.4框架中视图共享数据的方法详解
本文实例讲述了Laravel5.4框架中视图共享数据的方法.分享给大家供大家参考,具体如下: 每个人都会遇到这种情况:某些数据还在每个页面进行使用,比如用户信息,或者菜单数据,最基本的做法是在每个视图 ...
- Spring AOP 知识点入门
一.基本知识点 1.AOP概念 AOP(Aspect-Oriented Programming), 即 面向切面编程, 它与 OOP( Object-Oriented Programming, 面向对 ...
- Linux SSH 连接安全设置
一.更换端口, 可以在一定程度上防止扫描攻击 vim /etc/ssh/sshd_config 将 port 一项从 22 更改为高位端口, 然后重启 ssh 服务 systemctl restart ...
- 【Centos】桌面安装(转)
链接:https://www.bnxb.com/linuxserver/27457.html 正常我们在使用CENTOS时候都是不会去用到它的GUI桌面系统,都是用最基础的命令行形式,这样会比较节省服 ...
- 微服务学习之路(三)——实现RPC远程服务调用
RPC(Remote Producedure Call)调用原理:服务消费者称为客户端,服务提供者称为服务端,处于不同网络地址,需要建立网络连接.建立连接后,双方还必须按照某种约定的协议进行网络通讯— ...