题目:

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 (路径之和) 【面试算法题】的更多相关文章

  1. [leetcode] Path sum路径之和

    要求给定树,与路径和,判断是否存在从跟到叶子之和为给定值的路径.比如下图中,给定路径之和为22,存在路径<5,4,11,2>,因此返回true;否则返回false. 5 / \ 4 8 / ...

  2. [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 ...

  3. 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 ...

  4. [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 ...

  5. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

  10. python经典面试算法题1.3:如何计算两个单链表所代表的数之和

    本题目摘自<Python程序员面试算法宝典>,我会每天做一道这本书上的题目,并分享出来,统一放在我博客内,收集在一个分类中. 1.2 如何实现链表的逆序 [华为笔试题] 难度系数:⭐⭐⭐ ...

随机推荐

  1. pandas.Panel数据

    from pandas import Panel, DataFrame import numpy as np dd = {} for i in range(1, 3): name = 'X' + st ...

  2. Android 编程下两种方式注册广播的区别

    常驻型广播 常驻型广播,当你的应用程序关闭了,如果有广播信息来,你写的广播接收器同样的能接收到,它的注册方式就是在你应用程序的AndroidManifast.xml 中进行注册,这种注册方式通常又被称 ...

  3. GreenDao官方文档翻译(下)

    第五篇 查询 查询会返回符合某些特定标准的实体.你可以使用原始的SQL定制查询语句,或者更好的方式:使用GreenDao的QueryBuilder API.该查询也支持lazy-loading的结果集 ...

  4. Arduino "Card failed, or not present"(即找不到SD卡)错误解决方案

    http://forum.arduino.cc/index.php?topic=28763.0 Arduino SD自带的例程都是有一个BUG的,必须在pinMode后面加上如下的第二行代码 pinM ...

  5. (win+linux)双系统,删除linux系统的条件下,删除grub引导记录,恢复windows引导

    //(hdx,y) (显示查找到的分区号)第一个数字指第几个硬盘,第二个指第几个分区.   一般我们是(hd0,0) \n Linux的分区已经被你从Windows中删除,系统启动后停在“grub&g ...

  6. RandomAccessFile、FileChannel、MappedByteBuffer读写文件

    s package com.nio; import java.io.Closeable; import java.io.FileNotFoundException; import java.io.IO ...

  7. Oracle锁表(转载)

    锁定类型               行级锁               表级锁行级锁         ---- 行被排他锁定         ----在某行的锁被释放之前,其他用户不能修改此行    ...

  8. Cocos2d-android (03) 向量

    向量的基本运算及动作 import org.cocos2d.actions.interval.CCJumpBy; import org.cocos2d.actions.interval.CCMoveB ...

  9. TopFreeTheme精选免费模板【20130827】

    今天我们整理了一些关于WordPress, Joomla, Drupal, Magento, OpenCart的最新免费模板主题,绝大多数都是来自ThemeForest的响应式风格模板主题.题材多样化 ...

  10. 使用Sunny-grok实现内网转发

    Sunny-grok 申请地址:http://www.ngrok.cc ngrok.cfg配置: server_addr: "server.ngrok.cc:4443" auth_ ...