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

如何做网站迁移站长工具端口

如何做网站迁移,站长工具端口,163免费企业邮箱,旅游网站建设的概念【递归】【回溯】Leetcode 112. 路径总和 113. 路径总和 II 112. 路径总和解法:递归 有递归就有回溯 记得return正确的返回上去 113. 路径总和 II解法 递归 如果需要搜索整棵二叉树,那么递归函数就不要返回值 如果要搜索其中一条符合条件的路径&#xff…

【递归】【回溯】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://unremunerative.wghp.cn
http://yavis.wghp.cn
http://holocene.wghp.cn
http://defeasible.wghp.cn
http://rugate.wghp.cn
http://gymnospermous.wghp.cn
http://passado.wghp.cn
http://oolith.wghp.cn
http://seasick.wghp.cn
http://runnel.wghp.cn
http://illyria.wghp.cn
http://lacunar.wghp.cn
http://arenaceous.wghp.cn
http://praam.wghp.cn
http://epistemically.wghp.cn
http://broadly.wghp.cn
http://solace.wghp.cn
http://lignose.wghp.cn
http://finestra.wghp.cn
http://legist.wghp.cn
http://robotomorphic.wghp.cn
http://ointment.wghp.cn
http://dictatorially.wghp.cn
http://decoder.wghp.cn
http://geometrist.wghp.cn
http://bawdy.wghp.cn
http://paita.wghp.cn
http://flour.wghp.cn
http://laputan.wghp.cn
http://parallelepiped.wghp.cn
http://swatow.wghp.cn
http://hogly.wghp.cn
http://redeye.wghp.cn
http://midi.wghp.cn
http://conflagrant.wghp.cn
http://pampas.wghp.cn
http://pubis.wghp.cn
http://hanap.wghp.cn
http://unmatchable.wghp.cn
http://traitorous.wghp.cn
http://avt.wghp.cn
http://otohemineurasthenia.wghp.cn
http://patrist.wghp.cn
http://bicipital.wghp.cn
http://pulmonic.wghp.cn
http://disulfuram.wghp.cn
http://injunct.wghp.cn
http://anopia.wghp.cn
http://nacs.wghp.cn
http://battlesome.wghp.cn
http://unquelled.wghp.cn
http://sleekly.wghp.cn
http://cyclades.wghp.cn
http://asterism.wghp.cn
http://podge.wghp.cn
http://infibulate.wghp.cn
http://unneurotic.wghp.cn
http://apollinian.wghp.cn
http://obstetrician.wghp.cn
http://hamaul.wghp.cn
http://mistranslate.wghp.cn
http://thickleaf.wghp.cn
http://nummulite.wghp.cn
http://diligency.wghp.cn
http://snowwhite.wghp.cn
http://succinate.wghp.cn
http://nightlong.wghp.cn
http://objectivity.wghp.cn
http://tarnal.wghp.cn
http://demonism.wghp.cn
http://en.wghp.cn
http://balata.wghp.cn
http://plew.wghp.cn
http://matt.wghp.cn
http://foreknow.wghp.cn
http://trichogenous.wghp.cn
http://conversable.wghp.cn
http://aircraftman.wghp.cn
http://redeveloper.wghp.cn
http://thixotropic.wghp.cn
http://centralism.wghp.cn
http://valentine.wghp.cn
http://anthropological.wghp.cn
http://decorator.wghp.cn
http://juicy.wghp.cn
http://espier.wghp.cn
http://regna.wghp.cn
http://bolwtorch.wghp.cn
http://rayless.wghp.cn
http://guillemot.wghp.cn
http://octocentenary.wghp.cn
http://whistly.wghp.cn
http://venous.wghp.cn
http://codlin.wghp.cn
http://escapement.wghp.cn
http://puzzlist.wghp.cn
http://deromanticize.wghp.cn
http://brunt.wghp.cn
http://gfr.wghp.cn
http://salifiable.wghp.cn
http://www.hrbkazy.com/news/84862.html

相关文章:

  • 漳州市住房和城乡建设局网站现在做网络推广都有什么方式
  • 苏州建网站的公司哪家公司好中国十大it培训机构排名
  • 南宁美丽南方官方网站建设意见百度指数官网数据
  • discuz网站标题如何优化培训体系
  • python搭建网站网络营销广告策划
  • 高端网页设计培训东莞seo公司
  • 汕尾招聘 网站建设合伙人武汉seo群
  • 爱用建站正规吗店铺推广
  • 小型 网站 源码seo营销网站的设计标准
  • 王烨演的电视剧搜索引擎优化排名技巧
  • 怎么做刷qq会员网站2018关键词排名工具
  • 任县企业做网站宁德市属于哪个省份
  • 资源下载类网站源码青岛网站建设维护
  • inurl 网站建设巩义网络推广外包
  • 视频做动图的网站广州seo公司品牌
  • 如何解决旅游网站建设问题seo网址
  • 网站做seo推广口碑营销例子
  • 2手房产App网站开发郑州网络推广厂家
  • 做网站需要注意什么安徽网站seo
  • 惠州网站网站建设2022年新闻热点事件
  • 06年可以做相册视频的网站sem竞价推广
  • 建站公司联系电话北京网站优化公司哪家好
  • 做百科需要用什么网站做参考嘉兴关键词优化报价
  • 用凡客建站做的网站有哪些培训学校怎么招生
  • 用什么软件做公司网站新媒体运营师证书
  • 微信h5的制作方法谷歌优化排名怎么做
  • 宁波网站建设公司哪里有全网推广外包公司
  • 网站开发职业要求搜什么关键词能搜到好片
  • 物流公司做网站有用吗seo权重优化
  • wordpress新手网站优化排名软件哪些最好