Leetcode 112&113&437(Java)
来源:互联网 发布:glov卸妆巾 知乎 编辑:程序博客网 时间:2024/06/04 19:25
今天来总结一下Leetcode上目前我做到关于Path Sum即二叉树求和的内容,涉及的题目有三道,题号为112(easy),113(medium),437(easy)。
Leetcode 112
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.
用DFS结合递归来求解这道题目,用sum减去当前结点值,放入下一次递归的sum中,注意题中符合条件的路径是根到叶子结点,因此在判断是要注意叶子结点的判别。本题AC码如下:
public class Solution { public boolean hasPathSum(TreeNode root, int sum) { if(root==null)return false; if(sum==root.val && root.left==null && root.right==null)return true; return hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val); }}
Leetcode 113
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]
]
这道题目在112的基础上更进一步,要输出所有符合输出条件的路径。同样用递归,用一个List来存走过的结点,若符合条件,则将这个List存人List中的答案list中:
public class Solution { public List<List<Integer>> pathSum(TreeNode root, int sum){ List<List<Integer>> result = new LinkedList<List<Integer>>(); List<Integer> currentResult = new LinkedList<Integer>(); pathSum(root,sum,currentResult,result); return result; } public void pathSum(TreeNode root, int sum, List<Integer> currentResult, List<List<Integer>> result) { if (root == null) return; currentResult.add(new Integer(root.val)); if (root.left == null && root.right == null && sum == root.val) { result.add(new LinkedList(currentResult)); currentResult.remove(currentResult.size() - 1); return; } else { pathSum(root.left, sum - root.val, currentResult, result); pathSum(root.right, sum - root.val, currentResult, result); } currentResult.remove(currentResult.size() - 1); }}
Leetcode 437
这道题去掉了叶子结点的要求,返回了答案的个数而非详细内容。
public class Solution { public int pathSum(TreeNode root, int sum) { List<Integer> currentResult = new LinkedList<Integer>(); int result = 0; pathSum(root,sum,currentResult,result); return result; } public void pathSum(TreeNode root, int sum, List<Integer> currentResult, int result) { if (root == null) return; currentResult.add(new Integer(root.val)); if (sum == root.val) { result++; currentResult.remove(currentResult.size() - 1); return; } else { pathSum(root.left, sum - root.val, currentResult, result); pathSum(root.right, sum - root.val, currentResult, result); } currentResult.remove(currentResult.size() - 1); }}
总结
刚开始刷Leetcode的时候,对树的理解还是比较偏理论,多做些类似的题脑子里的思路会越来越清晰,希望等题量累积到足够量的时候可以自己来总结一篇树问题的求解SOP~
- Leetcode 112&113&437(Java)
- [leetcode-112]Path Sum(java)
- [Java]LeetCode 112Path Sum&113Path Sum II
- Leetcode(java)
- leetcode java
- 【LeetCode】#112 #113 #437 Path Sum Series
- [leetcode-113]Path Sum II(java)
- [LeetCode]Next Permutation java leetcode
- leetcode解题之112 & 113 & 437. Path Sum java版(二叉树路径和)
- LeetCode: Two Sum (Java)
- [Leetcode] Two Sum (Java)
- [Leetcode] ZigZag Conversion (Java)
- [Leetcode] Reverse Integer (Java)
- [Leetcode] Palindrome Number (Java)
- [Leetcode] Valid Parentheses (Java)
- [Leetcode] Generate Parentheses (Java)
- [Leetcode] Remove Element (Java)
- [Leetcode] Implement strStr() (Java)
- 【p1443-马的遍历】解题记录
- Java集合之Set集合
- 解决linux网络无法连接问题
- databing 踩的又一个坑
- 【CodeForces 630K】Indivisibility(容斥原理)
- Leetcode 112&113&437(Java)
- 算法设计作业8
- 立体视觉SLAM方法流程
- 最全的C、C++算法集合
- css--1.基础和选择器
- Linux下生成patch和打patch
- HTTP 2.0
- LeetCode067 Add Binary
- Ubuntu16.04 主题安装