当前位置: 首页 > news >正文

【递归】【回溯】Leetcode 112. 路径总和 113. 路径总和 II

【递归】【回溯】Leetcode 112. 路径总和 113. 路径总和 II

  • 112. 路径总和
    • 解法:递归 有递归就有回溯 记得return正确的返回上去
  • 113. 路径总和 II
    • 解法 递归

如果需要搜索整棵二叉树,那么递归函数就不要返回值
如果要搜索其中一条符合条件的路径,递归函数就需要返回值,因为遇到符合条件的路径了就要及时返回

112. 路径总和

---------------🎈🎈题目链接🎈🎈-------------------
在这里插入图片描述

解法:递归 有递归就有回溯 记得return正确的返回上去

count初始等于targetsum,逐次减,如果到了叶子结点正好count为0,那么就返回true
终止条件:if(root.left = null && root.right = null && count=0){ return true; }

时间复杂度O(N)
空间复杂度O(N)

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public boolean hasPathSum(TreeNode root, int targetSum) {// 终止条件if(root == null) return false;int count = targetSum-root.val;return help(root,count);}public boolean help(TreeNode root, int count){if(root.left==null && root.right==null && count==0){return true;}if(root.left==null && root.right==null && count!=0){return false;}// 左if(root.left != null){if(help(root.left, count-root.left.val)){return true;}}// 右if(root.right != null){if(help(root.right, count-root.right.val)){return true;}}return false;}}

113. 路径总和 II

---------------🎈🎈题目链接🎈🎈-------------------
在这里插入图片描述

解法 递归

时间复杂度O(N)
空间复杂度O(N)

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {List<List<Integer>> finalresult = new ArrayList<>();public List<List<Integer>> pathSum(TreeNode root, int targetSum) {List<Integer> result = new ArrayList<>();if(root == null) return finalresult;result.add(root.val);helper(root,targetSum-root.val,result);return finalresult;}public void helper(TreeNode root, int count, List<Integer> result){if(root.left == null && root.right==null && count==0){finalresult.add(new ArrayList<>(result)); // 这里千万不能finalresult.add(result) 这就成了添加result的引用,每次都会变}// 左if(root.left != null){result.add(root.left.val);helper(root.left, count-root.left.val,result);result.remove(result.size()-1); // 回溯}// 右if(root.right != null){result.add(root.right.val);helper(root.right,count-root.right.val,result);result.remove(result.size()-1); // 回溯}}
}
http://www.lryc.cn/news/307584.html

相关文章:

  • AxureCloud配置文件详细介绍
  • Centos开机网卡自启动失败
  • 华为OD技术面试案例3-2024年
  • 全面升级!Apache HugeGraph 1.2.0版本发布
  • WinCC如何与三菱Q系列PLC进行以太网通讯
  • Spring11、整合Mybatis
  • C语言练习:(力扣645)错误的集合
  • 广和通发布基于MediaTek T300平台的RedCap模组FM330系列及解决方案
  • 代码随想录训练营第六十三天打卡|503.下一个更大元素II 42. 接雨水
  • 【web】nginx+php环境搭建-关键点(简版)
  • 1、什么是ETF?
  • 备战蓝桥杯Day18 - 双链表
  • 【大数据】Flink 内存管理(二):JobManager 内存分配(含实际计算案例)
  • (2024,Sora 逆向工程,DiT,LVM 技术综述)Sora:大视觉模型的背景、技术、局限性和机遇回顾
  • MySQL基础(二)
  • el-table 多选表格存在分页,编辑再次操作勾选会丢失原来选中的数据
  • 备战蓝桥杯————如何判断回文链表
  • linux 文本编辑命令【重点】
  • C#面:ref 和 out 的区别
  • php脚本输出中文在浏览器中显示乱码
  • 【Unity每日一记】角色控制器Character Contorller
  • Kafka入门介绍一
  • leetcode 3.反转链表;
  • 【蓝桥杯】快读|min和max值的设置|小明和完美序列|​顺子日期​|星期计算|山
  • 半小时到秒级,京东零售定时任务优化怎么做的?
  • stm32——hal库学习笔记(ADC)
  • 一周学会Django5 Python Web开发-Http请求HttpRequest请求类
  • element el-date-picker 日期组件置灰指定日期范围、禁止日期范围日期选择
  • 202434读书笔记|《繁星·春水》——残花缀在繁枝上,鸟儿飞去了,撒得落红满地,生命也是这般的一瞥么?
  • Golang 关于 interface 接口的理解