leetcode:Path 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.
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.
题意判断从根节点到叶子节点的路径和是否有等于sum的值。
叶子节点就是没有子节点的节点,判断叶子节点的当前值是否等于当前的sum。
若不是叶子节点,将当前sum减去当前节点的值,递归求解。
/**
* 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 hasPathSum(TreeNode *root, int sum) {
if(!root)return false;
if(sum==root->val&&root->left==NULL&&root->right==NULL)return true;
return (root->left && hasPathSum(root->left,sum-root->val) )
||(root->right && hasPathSum(root->right,sum-root->val));
}
};
// blog.csdn.net/havenoidea
leetcode:Path Sum (路径之和) 【面试算法题】的更多相关文章
- [leetcode] Path sum路径之和
要求给定树,与路径和,判断是否存在从跟到叶子之和为给定值的路径.比如下图中,给定路径之和为22,存在路径<5,4,11,2>,因此返回true;否则返回false. 5 / \ 4 8 / ...
- [Leetcode] 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:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
- [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 su ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 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] Path Sum IV 二叉树的路径和之四
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- [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] Path Sum II路径和
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- python经典面试算法题1.3:如何计算两个单链表所代表的数之和
本题目摘自<Python程序员面试算法宝典>,我会每天做一道这本书上的题目,并分享出来,统一放在我博客内,收集在一个分类中. 1.2 如何实现链表的逆序 [华为笔试题] 难度系数:⭐⭐⭐ ...
随机推荐
- 关于ios越狱开发的那些事
也许吧,每每接触某些新东西的时候,都有点犯晕吧,这不是应该要的. 第一次接触ios越狱开发,也是这样吧.这篇主要是从无到有的说一下ios越狱的开发,网上很多的教程大部门都比较旧了吧,放在新设备上总是出 ...
- ecshop 分类树全部显示
1.在page_header.lbi文件中加入 $GLOBALS['smarty']->assign('categories',get_categories_tree()); // 保证首页页面 ...
- 【英语】Bingo口语笔记(7) - Break系列
- 【英语】Bingo口语笔记(24) - L的发音技巧
舌头往上跑
- 细数Android开源项目中那些频繁使用的并发库中的类
这篇blog旨在帮助大家 梳理一下前面分析的那些开源代码中喜欢使用的一些类,这对我们真正理解这些项目是有极大好处的,以后遇到类似问题 我们就可以自己模仿他们也写 出类似的代码. 1.ExecutorS ...
- 对于REST中无状态(stateless)的一点认识
今天早上在Yahoo的邮件列表里看到一篇颇有意思的讨论,标题为RESTful vs. unRESTful: Session IDs and Authentication(51CTO编者注:意为REST ...
- Asp.net 身份验证方式?
[Forms 身份验证] 通过其可将没有通过身份验证的请求重定向到使用 HTTP 客户端重定向的 HTML 窗体的系统.用户提供凭据并提交该窗体.如果应用程序验证该请求,系统就会发出包含凭据或密钥的 ...
- Android 校验apk文件渠道号、包名、版本号
功能:可查看单个或目录下所有apk文件的渠道号.包名.版本号 下载地址:http://download.csdn.net/detail/zgz345/9248487使用:以查看包名.版本号为例 1.c ...
- mysql优化SQL语句的一般步骤及常用方法
一.优化SQL语句的一般步骤 1. 通过show status命令了解各种SQL的执行频率 mysqladmin extended-status 或: show [session|global]sta ...
- 长轮询和Comet
长轮询方式是由前端定时发起AJAX请求,若请求到数据则把数据显示出来. comet方式是由客户端与服务器端发起一个长连接,然后客户端通过监听事件的方式,来对服务器端返回的数据作出响应和处理. 实时性要 ...