一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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.

(二)解题

题目大意:给定一个二叉树和一个整数,求二叉树是否存在一个根节点到叶子节点的路径,使得该路径上的所有节点的值加起来等于该整数。

解题思路:递归,碰到叶子节点就算路径上的节点值的和,判断等不等于该整数。

/**
 * 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;//树为空返回false
        bool flag = false;
        dfsTree(root,sum,0,flag);//递归函数
        return flag;
    }
    //sum为要求的路径和
    //cur为当前路径和
    //flag为是否满足cur==sum
    void dfsTree(TreeNode* root, int& sum,int cur,bool& flag)
    {
        if(root->left == NULL && root->right==NULL) if(!flag) flag=(sum==(cur+root->val));//此时为叶子节点,判断路径和等不等于sum
        if(root->left!=NULL) dfsTree(root->left,sum,cur+root->val,flag);//往左子树搜索
        if(root->right!=NULL) dfsTree(root->right,sum,cur+root->val,flag);//往右子树搜索
    }
};

【一天一道LeetCode】#112. Path Sum的更多相关文章

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

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

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

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

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

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

  7. leetcode 112 Path Sum ----- java

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

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

  9. [Leetcode]112. Path Sum -David_Lin

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  10. (二叉树 DFS 递归) 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 ...

随机推荐

  1. Splay讲解

    Splay讲解 Splay是平衡树的一种,是一种二叉搜索树,我们先讲解一下它的核心部分. Splay的核心部分就是splay,可能有些人会说什么鬼?这样讲解是不是太不认真了?两个字回答:不是.第一个S ...

  2. strings.h 与 string.h 头文件的区别

    今天使用 man string 来查看 string 文件的使用的方法(毕竟里面的函数名字和传入参数和发挥参数的类型,如果一段时间不使用,会产生遗忘.) 偶然发现,string.h 的man page ...

  3. Spring中@Autowired与@Resource的区别

    1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必 ...

  4. 利用css3+js实现简单带立体过渡效果的图片切换(chrome浏览器)

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  5. 自调用匿名函数和js的Module模式

    编写自调用匿名函数的结构一般如下: :(function( window, undefined ) { // your code })(window); 传入的参数window,和参数列表中的unde ...

  6. Noip2017 普及 T3 Chess

    神奇的场上原码 #include<iostream> #include<cstdio> #include<cstring> #include<queue> ...

  7. python学习之路网络编程篇(第二篇)

    新课程知识的引入:python作用域 #python中无块级别作用域 if 1 == 1 : name = 'alex' print(name) for i in range(10): name = ...

  8. [转载]致创业者:APP已死 服务永生

    前几日,有位创业者和我讲他在带领团队做一个将爱踢球的人集中在一起的App,我告诉他你的创业方向错了.原因在于你的目的是要为爱踢球的人提供服务,而你现在却在竭尽全力的做App,你应该做的是设计你为爱踢球 ...

  9. 自定义View总结2

    自定义控件: 1.组合控件:将系统原生控件组合起来,加上动画效果,形成一种特殊的UI效果 2.纯粹自定义控件:继承自系统的View,自己去实现view效果 优酷菜单: 1.系统原生的旋转和位置动画并没 ...

  10. 使用java操作HDFS

    新建Java Project; 1,右击项目,属性,Java Build Path,Libraries,Add External JARs(haddopp根目录下的所以jar): 2,做一下项目关联, ...