一天一道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. win10利用自带的IIS搭建ftp遇到瓶颈,离线求解!!!

  2. substr和substring的区别

    substr和substring两个都是截取字符串的. 两者有相同点,如果只是写一个参数,两者的作用都是一样的:就是截取字符串当前下标以后直到字符串最后的字符串片段. 例如:`var a=”abcde ...

  3. redis分布式锁-SETNX实现

    Redis有一系列的命令,特点是以NX结尾,NX是Not eXists的缩写,如SETNX命令就应该理解为:SET if Not eXists.这系列的命令非常有用,这里讲使用SETNX来实现分布式锁 ...

  4. 浅析java内存管理机制

    内存管理是计算机编程中的一个重要问题,一般来说,内存管理主要包括内存分配和内存回收两个部分.不同的编程语言有不同的内存管理机制,本文在对比C++和Java语言内存管理机制的不同的基础上,浅析java中 ...

  5. 听说图像识别很难,大神十行代码进行Python图像识别

      随着深度学习算法的兴起和普及,人工智能领域取得了令人瞩目的进步,特别是在计算机视觉领域.21世纪的第二个十年迅速采用卷积神经网络,发明了最先进的算法,大量训练数据的可用性以及高性能和高性价比计算的 ...

  6. Unity使用C++作为游戏逻辑脚本的研究

    文章申明:本文来自JacksonDunstan的博客系列文章内容摘取和翻译,版权归其所有,附上原文的链接,大家可以有空阅读原文:C++ Scripting( in Unity) 一.C#和C++的通信 ...

  7. 嫌我的键盘的backspace太小,就尝试了一下改键工具--keyTweak

    KeyTweak是一个很简单的键盘remap小工具,主要功能就是可以让我们选择某个按键并重新赋予该按键一个新的功能.如果哪天你的键盘某个重要的键坏掉了,可以通过这个免费的软件来重新定义该按键的功能.譬 ...

  8. delphi 线程教学第五节:多个线程同时执行相同的任务

    第五节:多个线程同时执行相同的任务   1.锁   设,有一个房间 X ,X为全局变量,它有两个函数  X.Lock 与 X.UnLock; 有如下代码:   X.Lock;      访问资源 P; ...

  9. Tomcat7源码环境搭建

    一.下载Tomcat7源码 从官网上下载Tomcat源码,   http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.70/src/apache-t ...

  10. Hadoop2动态调整Log级别-以datanode的heartbeat log为例

    在Hadoop中,有些log信息在正常情况下是不打印出来的.比如datanode发送heartbeat的日志. 代码位于BPServiceActor#sendHeartBeat方法中,如下图: 由于默 ...