Path Sum
来源:互联网 发布:模仿声音软件下载 编辑:程序博客网 时间:2024/06/08 01:05
Notes:不太会用递归啊
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public List<Integer> list = new ArrayList<Integer>(); public void getLength(TreeNode root, int treeLength){ if(root.left == null && root.right == null){ list.add(treeLength + root.val); } if(root.left != null){ getLength(root.left, treeLength + root.val); } if(root.right != null){ getLength(root.right, treeLength + root.val); } } public boolean hasPathSum(TreeNode root, int sum) { int treeLength = 0; if(root == null){ return false; } getLength(root, treeLength); for(int i = 0; i < list.size(); i++) { if(list.get(i) == sum){ return true; } } return false; }}简单的递归方法:
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public boolean hasPathSum(TreeNode root, int sum) { int treeLength = 0; if(root == null){ return false; } if(root.left == null && root.right == null) return root.val == sum; return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val); }}
0 0
- Path Sum && Path Sum ||
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- 如何使用c#上传图片,图片是base64编码的字符串
- Javascript学习笔记5 - 滑动Slides
- TiXmlDeclaration TiXmlDeclaration LinkEndChild
- Android Studio常用操作技巧(不断更新)
- Linux系统下如何查看物理内存占用率
- Path Sum
- Date类学习总结(Calendar Date 字符串 相互转换 格式化)
- android 基础知识总结
- 2015暑期实习生招聘阿里一面面试题之如何判判树是否为二叉搜索树 如何利用前序中序建二叉树
- hadoop(1)
- 在开发的时候脚本语言用的最多了,写点JavaScript的一写需要注意的地方 后续
- Pinyin4j入门教程---识别中文
- .NET MVC 图片上传
- 深度定制 UIButton 按钮