【一天一道LeetCode】#112. Path Sum

时间:2022-09-02 10:21:41

一天一道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. Matlab的实时编辑器(Live Script)

    Matlab的实时编辑器(Live Script) 看到伟哥换用了matlab2016a,里面集成了acf行人检测算法,感觉挺好的,索性也从matlab2014a跨越到matlab2016a. 安装后 ...

  2. Flip Game poj1753

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32961   Accepted: 14407 Description Fli ...

  3. C#线程系列讲座(5):同步技术之Monitor

    在上一讲介绍了使用lock来实现线程之间的同步.实际上,这个lock是C#的一个障眼法,在C#编译器编译lock语句时,将其编译成了调用Monitor类.先看看下面的C#源代码: public sta ...

  4. C#_deepCopy

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Run ...

  5. java中Hashtable中的t为什么是小写(转)

    因为在很多年前刚学java的时候用到Hashtable的时候比较好奇为什么第二个t是小写,这不符合sun的风格啊,整个jdk都是标准驼峰,于是带着这个疑问翻过 很多书,看多很多资料,最后的结论是: H ...

  6. web报告工具FineReport在使用方法和解决方案常见错误遇到(一)

    FineReport在使用方法和解决方案常见错误遇到(一) 这里写的开胃菜.我希望我们能理清自己的问题和解决办法干出来的,Mark一点点.有利于所有. 失败搜索出,如果有一个文件,看看你的度娘那里.看 ...

  7. jsonp原来是这么回事,豁然开朗

    什么是JSONP 先说说JSONP是怎么产生的: 其实网上关于JSONP的讲解有很多,但却千篇一律,而且云里雾里,对于很多刚接触的人来讲理解起来有些困难,小可不才,试着用自己的方式来阐释一下这个问题, ...

  8. ms10_046_shortcut_icon_dllloader漏洞利用和ettercap dns欺骗

    ms10_046_shortcut_icon_dllloader漏洞利用过程 msf > use exploit/windows/browser/ms10_046_shortcut_icon_d ...

  9. 【转】AI类人工智能产品经理的丛林法则

    本文转载自:https://blog.csdn.net/buptgshengod/article/details/77030338 AI是大家都很关注的领域,然而对于大部分想要入行的同学来讲,AI的算 ...

  10. 【转】【WPF】WPF为stackpanel设置滚动条

    <ScrollViewer x:Name="scrolls" VerticalScrollBarVisibility="Auto" HorizontalS ...