leetcode: Path Sum
来源:互联网 发布:android入门编程视频 编辑:程序博客网 时间:2024/06/05 17:01
思路你较简单,就是dfs前序遍历二叉树找到判断是否存在符合要求的路径。
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { boolean res = false; public boolean hasPathSum(TreeNode root, int sum) { if( root == null ) { return res; } fun(root,sum); return res; } void fun( TreeNode root,int sum ) { sum = sum-root.val; if( res == true ) { return ; } if( root.right==null && root.left == null ) { if( sum == 0 ) { res = true; } return ; } boolean f1=true,f2=true; if( root.left != null ) { fun(root.left,sum); } if( root.right != null ) { fun(root.right,sum); } }}
0 0
- leetcode Path Sum && Path Sum ||
- 【Leetcode】Path Sum (Sum)
- leetcode:leetcode :Path Sum
- 【Leetcode】Path Sum II (Sum)
- LeetCode: Minimum Path Sum
- LeetCode: Path Sum
- LeetCode: Path Sum II
- LeetCode Minimum Path Sum
- LeetCode Path Sum
- LeetCode Path Sum II
- [Leetcode] Path Sum
- [Leetcode] Path Sum II
- LeetCode : Minimum Path Sum
- [Leetcode] Minimum Path Sum
- LeetCode: Path Sum
- LeetCode: Path Sum II
- [LeetCode] Path Sum
- [LeetCode] Path Sum II
- 怎样才能不浪费生命
- 【原创】动态图像监测开源代码 motion 学习 ----- Motion源码分析(1)
- Machine Learning(Andrew)Week10(下)
- TOP 10开源的推荐系统简介
- 线段树
- leetcode: Path Sum
- HttpClient4.3教程 第三章 Http状态管理
- 导出excel,最前面数字0不见了
- SMTP发送
- Delphi中MessageBox用法
- MongoDB创始人:项目经理应该把30%的时间用在编程上
- the use of mmap
- 黑马程序员------毕老师视频笔记第六天------面向对象(static关键字)
- combobox重绘代码笔记