简单题

I

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.

public class Solution {public boolean hasPathSum(TreeNode root, int sum) {if(root==null) return false;if(root.val==sum && root.left==null && root.right==null) return true;return (hasPathSum(root.left, sum-root.val) || hasPathSum(root.right, sum-root.val));}
}

II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

For example:
Given the below binary tree and sum = 22,

              5/ \4   8/   / \11  13  4/  \    / \7    2  5   1

return

[[5,4,11,2],[5,8,4,5]
]

典型dfs题目
public class Solution {public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) {ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();ArrayList<Integer> cl = new ArrayList<Integer>();dfs(res, cl, root, sum);return res; }private void dfs(ArrayList<ArrayList<Integer>> res, ArrayList<Integer> cl,TreeNode n, int s ){if(n==null) return;cl.add(n.val);if(n.left==null && n.right==null && n.val==s){res.add(new ArrayList<Integer>(cl)); // if dont rebuild, will make conflict with the last line of cl.remove 哦!}else{if(n.left!=null)dfs(res, cl, n.left,s-n.val);if(n.right!=null)dfs(res, cl, n.right,s-n.val);}cl.remove(cl.size()-1);}
}

转载于:https://www.cnblogs.com/jiajiaxingxing/p/4565053.html

Path Sum I, II相关推荐

  1. [LeetCode]113.Path Sum II

    [题目] Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the giv ...

  2. 113. Path Sum II

    /** 113. Path Sum II * 11.18 By Mingyang* 典型的backtracking,不过注意,这里的值可能是负数,所以不能用sum小于0来做任何判断* 1.长度标准:无 ...

  3. leetcode 112. Path Sum, 113. Path Sum II | 112,113. 路径总和 I, II(Java)

    题目 https://leetcode.com/problems/path-sum/ https://leetcode.com/problems/path-sum-ii/ 题解 简单的遍历二叉树,不解 ...

  4. 63. Unique Paths II and 64. Minimum Path Sum

    文章目录 1 63 Unique Paths II 1.1 题目描述 1.2 动态规划解决 2 64. Minimum Path Sum 2.1 题目理解 2.2 动态规划 这一遍刷dp的题目就很轻松 ...

  5. Leetcode: mimimum depth of tree, path sum, path sum II

    思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...

  6. LeetCode 113. Path Sum II

    113. Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum eq ...

  7. Path Sum II leetcode java

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  8. [Leetcode] Path Sum II路径和

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  9. [leetcode] Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

最新文章

  1. 梯度下降法——得到的结果可能是局部最优值,如果凸函数则可保证梯度下降得到的是全局最优值...
  2. 2018区块链生存指南:要做飞行的猪、摔不坏的弹球、未来的种子
  3. Python数据分析学习笔记之Pandas入门
  4. 简单的总是好的,在这个复杂的世界: java simple log
  5. vb鼠标涂鸦板的制作
  6. 使用C#实现网络时间同步功能
  7. Controller接口控制器详解(1)
  8. 某计算机有五级中断L4,2011计算机基础考研组成原理部分
  9. 《编码的奥秘》记录(二)
  10. 电脑测试耗电量软件,有测验电脑耗电量的软件么 ?
  11. 【计算机组成原理之存储系统】超级详细
  12. 由对称性知定点一定在x轴上_2021版江苏高考数学一轮复习讲义:第8章 第10节 圆锥曲线中的证明、探索性问题 Word版含答案...
  13. seqkit根据基因id_西番莲内参基因及其筛选方法和应用与流程
  14. 云计算网络,在数据中心之中主要有哪些优势?
  15. 计算机专业如何推广太极拳,太极拳在年轻人群体中推广的怎么样不得不说挺吃惊...
  16. 非零基础自学Golang 第3章 基本变量与类型 3.1 Go语言变量
  17. 关于MNN中图像预处理
  18. WordArt怎样生成中文词云?
  19. 一、python入门整体快速学习
  20. java75-GUL文本框和标签

热门文章

  1. c语言调用android surface,Android GUI SurfaceFlinger
  2. 神经网络的激活函数、并通过python实现激活函数
  3. Oracle 数据怎么实时同步到 Elasticsearch | 亲测干货建议收藏
  4. c语言程序设计实验实训教程公众号,C语言程序设计基础知道答案公众号
  5. 放大镜_屏幕放大镜怎么样使用方法
  6. java创建线程几种_java中创建线程有几种方式
  7. SpringBoot找不到或无法加载主类
  8. android 自定义进度条_第一百八十九回:Android中自定义ProgressBar三
  9. oracle将千万行查询优化到一秒内,oracle下一条SQL语句的优化过程(比较详细)
  10. php怎么实现匿名评论,PHP-匿名对象与匿名类的实现过程-0905