码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode-104. Maximum Depth of Binary Tree [C++][Java]


    LeetCode-437. Path Sum IIILevel up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.https://leetcode.com/problems/maximum-depth-of-binary-tree/

    Given the root of a binary tree and an integer targetSum, return the number of paths where the sum of the values along the path equals targetSum.

    The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).

    Example 1:

    Input: root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
    Output: 3
    Explanation: The paths that sum to 8 are shown.
    

    Example 2:

    Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
    Output: 3
    

    Constraints:

    • The number of nodes in the tree is in the range [0, 1000].
    • -10^9 <= Node.val <= 10^9
    • -1000 <= targetSum <= 1000

    【C++】

    1. /**
    2. * Definition for a binary tree node.
    3. * struct TreeNode {
    4. * int val;
    5. * TreeNode *left;
    6. * TreeNode *right;
    7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
    8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    10. * };
    11. */

    1. 双递归

    小心数据溢出

    1. class Solution {
    2. public:
    3. int pathSum(TreeNode* root, int targetSum) {
    4. if (root == nullptr) {return 0;}
    5. return pathSumStartWithRoot(root, targetSum)
    6. + pathSum(root->left, targetSum)
    7. + pathSum(root->right, targetSum);
    8. }
    9. int pathSumStartWithRoot(TreeNode* root, int sum) {
    10. if (!root) {return 0;}
    11. int count = root->val == sum? 1: 0;
    12. if (double(sum) - double(root->val) < INT_MIN) {return 0;}
    13. int remaingSum = sum - root->val;
    14. count += pathSumStartWithRoot(root->left, remaingSum);
    15. count += pathSumStartWithRoot(root->right, remaingSum);
    16. return count;
    17. }
    18. };

    2.递归+回溯

    1. class Solution {
    2. public:
    3. int pathSum(TreeNode* root, int targetSum) {
    4. int pathCount = 0;
    5. vector<int> currentPath;
    6. pathSumHelper(root, targetSum, currentPath, pathCount);
    7. return pathCount;
    8. }
    9. void pathSumHelper(TreeNode *root,
    10. int sum ,
    11. vector<int> ¤tPath,
    12. int &pathCount) {
    13. if(!root) return;
    14. currentPath.push_back(root->val);
    15. double currentSum = 0;
    16. for(int j=currentPath.size()-1; j>=0; j--) {
    17. currentSum += (double)currentPath[j];
    18. if(currentSum == sum) pathCount++;
    19. }
    20. pathSumHelper(root->left, sum, currentPath, pathCount);
    21. pathSumHelper(root->right, sum, currentPath, pathCount);
    22. currentPath.pop_back();
    23. }
    24. };

    【Java】

    1. /**
    2. * Definition for a binary tree node.
    3. * public class TreeNode {
    4. * int val;
    5. * TreeNode left;
    6. * TreeNode right;
    7. * TreeNode() {}
    8. * TreeNode(int val) { this.val = val; }
    9. * TreeNode(int val, TreeNode left, TreeNode right) {
    10. * this.val = val;
    11. * this.left = left;
    12. * this.right = right;
    13. * }
    14. * }
    15. */

    1. 双循环

    1. class Solution {
    2. public int pathSum(TreeNode root, int targetSum) {
    3. if (root == null) {return 0;}
    4. return pathSumHelper(root, targetSum)
    5. + pathSum(root.left, targetSum)
    6. + pathSum(root.right, targetSum);
    7. }
    8. public int pathSumHelper(TreeNode root, int sum) {
    9. if (root == null) {return 0;}
    10. int count = sum == root.val ? 1 : 0;
    11. if (Double.valueOf(sum)
    12. - Double.valueOf(root.val)
    13. < Integer.MIN_VALUE) {
    14. return 0;
    15. }
    16. int remaining = sum - root.val;
    17. return count
    18. + pathSumHelper(root.left, remaining)
    19. + pathSumHelper(root.right, remaining);
    20. }
    21. }

     参考文献

    【1】java中int转换double类型_Java 程序将int类型变量转换为double

  • 相关阅读:
    nodejs+vue 大学生就业管理系统
    【Linux常用命令14】Linux系统监控常用命令
    Windows 根据dll生成 lib文件
    Spring In Action 5 学习笔记 chapter1 拓展
    Ubuntu部署OpenStack踩坑指南:还要看系统版本?
    window10 mysql zip 安装
    Mac安装telnet
    Vue3修改Element-plus语言与项目国际化
    R语言ggplot2可视化:使用ggplot2可视化散点图、使用labs参数自定义X轴的轴标签文本(customize X axis labels)
    内衣洗衣机和手洗哪个干净?迷你洗衣机品牌推荐
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126239920
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号