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 如何实现链表的逆序 [华为笔试题] 难度系数:⭐⭐⭐ ...
随机推荐
- ecshop 改变sitemap.xml的位置
大家知道ECSHOP默认的sitemap.xml文件是放置在data文件夹中的,但是这不利于GOOGLE的抓取.我们必须把sitemap.xml文件放置在根目录下 在admin/sitemap.php ...
- swun 1612 合并果子
//思路:这题思路似乎很简单,每次取出最小的两个堆合并, //但是由于数据太大,不能采取每次进行排序的方式,所以 //想到用优先队列,以数据小的优先级更高为标准,但是 //优先队列中的数据默认情况 ...
- 《Unix网络编程》卷2 读书笔记 第2章- Posix IPC
1. 概述 Posix IPC 包括:Posix消息队列.Posix信号量.Posix共享内存区 Posix IPC在访问它们的函数和描述它们的信息上有一些类似点. 本章讲述所有这些共同属性:用于标识 ...
- 服务器中判断客户端socket断开连接的方法
1, 如果服务端的Socket比客户端的Socket先关闭,会导致客户端出现TIME_WAIT状态,占用系统资源. 所以,必须等客户端先关闭Socket后,服务器端再关闭Socket才能避免TIME_ ...
- Android 最火框架XUtils之注解机制详解
http://blog.csdn.net/rain_butterfly/article/details/37931031
- 13、NFC技术:读写非NDEF格式的数据
MifareUltralight数据格式 将NFC标签的存储区域分为16个页,每一个页可以存储4个字节,一个可存储64个字节(512位).页码从0开始(0至15).前4页(0至3)存储了NFC标签相关 ...
- VS2013 调试MVC源码[MVC5.2.3+MVC4Web项目]
1.目前MVC源码版本为5.2.3,下回来后用VS2013打开,把System.Web.Mvc项目的版本号改为4.0.0.1 2.在解决方案下建一个MVC4项目,.NET选4.5,修改根目录以及Vie ...
- winform form
WinForm:Windows Form,.Net中用来开发Windows窗口程序的技术,无论是之前学的控制台程序,还是后面要学的asp.net都是调用.net框架,因此所有知识点都是一样的.新建一个 ...
- CSS中animate属性
我记得,在工作中直接使用animation,只要能做出动画就完了,根本没有看每一个细节. 其实,这样做对于我们来说,的确没有错,因为工作中没有时间给你看每一个细节,大致看一篇就没下文了. 当我们想要好 ...
- 转-问自己:UI设计注意的十个问题
UI 设计需要自问的 10个问题 UI 设计的魅力在于,你不仅需要适当的技巧,更要理解用户与程序的关系.一个有效的用户界面关注的是用户目标的实现,包括视觉元素与功能操作在内的所有东西都需要完整一致 ...