原题链接:https://leetcode.com/problems/path-sum/description/
二叉树相关问题,直接深度优先搜索走一波:

/*** Created by clearbug on 2018/2/26.*/
public class Solution {static class TreeNode {int val;TreeNode left;TreeNode right;public TreeNode(int val) {this.val = val;}}public static void main(String[] args) {Solution s = new Solution();// test1TreeNode root = new TreeNode(3);root.left = new TreeNode(9);root.left.left = new TreeNode(33);root.right = new TreeNode(20);root.right.left = new TreeNode(15);root.right.right = new TreeNode(7);System.out.println(s.hasPathSum(root, 45));System.out.println(s.hasPathSum(root, 38));System.out.println(s.hasPathSum(root, 30));System.out.println(s.hasPathSum(root, 46));System.out.println(s.hasPathSum(root, 20));}public boolean hasPathSum(TreeNode root, int sum) {if (root == null) {return false;}return dfs(root, sum, 0);}private boolean dfs(TreeNode node, int sum, int currentSum) {currentSum += node.val;if (node.left == null && node.right == null) {if (currentSum == sum) {return true;}} else {if (node.left != null) {if (dfs(node.left, sum, currentSum)) {return true;}}if (node.right != null) {if (dfs(node.right, sum, currentSum)) {return true;}}}return false;}}

转载于:https://www.cnblogs.com/optor/p/8585521.html

112. Path Sum相关推荐

  1. 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/ 题解 简单的遍历二叉树,不解 ...

  2. [勇者闯LeetCode] 112. Path Sum

    [勇者闯LeetCode] 112. Path Sum Description Given a binary tree and a sum, determine if the tree has a r ...

  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 ...

  4. Leet Code OJ 112. Path Sum [Difficulty: Easy]

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  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 ...

  6. leetcode python3 简单题112. Path Sum

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十二题 (1)题目 英文: Given a binary tree and ...

  7. 112. Path Sum 路径总和

    Title 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 su ...

  8. [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 ...

  9. Leetcode | Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

最新文章

  1. Python CRC32 文件校验
  2. java i 原子_为什么i ++不是原子的?
  3. TI-BLE协议栈的GATT
  4. python中break、continue 、exit() 、pass终止循环的区别
  5. Divan and Kostomuksha (H version) dp,gcd(2300)
  6. 杂项:高考填报志愿综合参考系统
  7. PW Live 直播 | 北邮博士生纪厚业:异质图神经网络之模型和应用
  8. python中da_Python中字符的编码与解码
  9. Adobe和苹果相互推诿 不支持Flash谁之过?
  10. mysql标识列从一开始_mysql中标识列是什么意思有什么用
  11. VO,BO,PO,DO,DTO的区别
  12. lambda,reserve list, list comprehension, string of slice
  13. c++ websocket客户端_ESP32 Arduino教程:Websocket客户端
  14. 切换Pycharm的Python版本
  15. 介绍一些预言性质的梦
  16. UpdateProgress使用,出不来特效问题小结
  17. visio 2003 问题
  18. C++ 字符串逆序输出
  19. MathType数学公式编辑器,编辑数学资料工具
  20. android播放3gp格式,Android – 无法播放任何视频(mp4 / mov / 3gp /等)?

热门文章

  1. wxPython 笔记(8)设定窗体的样式
  2. 计算机专业人士,必读之经典图书
  3. Remix:高分辨率目标检测,面向边缘设备、可调谐
  4. 双十一囤点知识干货!
  5. ECCV2020 Spotlight | 图像定位上的细粒化区域相似性自监督
  6. ICCV 2019 | 旷视研究院提出文字检测新方法:像素聚合网络PAN
  7. ECCV18|这篇论文开源的车牌识别系统打败了目前最先进的商业软件(附Github地址)...
  8. 基于OpenCV与Dlib的行人计数开源实现
  9. 零基础应该先学习 java、php、前端 还是 python?
  10. 你想入门Python,还是得看这篇文章