LeetCode 113
问题:给你二叉树的根节点root和一个整数目标和targetSum,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。
示例
输入:root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
输出:[[5,4,11,2],[5,8,4,5]]
我的代码如下
1 class Solution { 2 public void traversal(TreeNode root, int count, List<List<Integer>> res, List<Integer> path) { 3 path.add(root.val); 4 if (root.left == null && root.right == null) { 5 if (count - root.val == 0) { 6 res.add(path); 7 } 8 return; 9 } 10 ? 11 if (root.left != null) { 12 traversal(root.left, count - root.val, res, path); 13 path.remove(path.size() - 1); 14 } 15 if (root.right != null) { 16 traversal(root.right, count - root.val, res, path); 17 path.remove(path.size() - 1); 18 } 19 } 20 ? 21 public List<List<Integer>> pathSum(TreeNode root, int targetSum) { 22 List<List<Integer>> res = new ArrayList<>(); 23 List<Integer> path = new ArrayList<>(); 24 if (root == null) return res; 25 traversal(root, targetSum, res, path); 26 ? 27 return res; 28 } 29 }
该题的思路是采用递归,traversal函数内root是当前树的根节点,count是目标值,res是存储结果,path是路径。该代码对于示例的输入输出为
1 输入:root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 2 输出:[[5],[5]]
经过排查最终问题在于代码中的add方法
原代码部分内容为
1 if (root.left == null && root.right == null) { 2 if (count - root.val == 0) { 3 res.add(path); 4 } 5 return; 6 }
该部分内容需要改为
1 if (root.left == null && root.right == null) { 2 if (count - root.val == 0) { 3 res.add(new ArrayList(path)); 4 } 5 return; 6 }
此时所有代码对于示例的输入输出为
1 输入:root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 2 输出:[[5,4,11,2],[5,8,4,5]]
在java中,存在8大基本数据类型,且均有对应的包装类
数据类型 | 占用位数 | 默认值 | 包装类 |
---|---|---|---|
byte(字节型) | 8 | 0 | Byte |
short(短整型) | 16 | 0 | Short |
int(整型) | 32 | 0 | Integer |
long(长整型) | 64 | 0.0l | Long |
float(浮点型) | 32 | 0.0f | Float |
double(双精度浮点型) | 64 | 0.0d | Double |
char(字符型) | 16 | '/u0000' | Character |
boolean(布尔型) | 1 | false | Boolean |
在java中,函数传递只有值传递,是指在调用函数时,将实际参数复制一份传递给函数,这样在函数中修改参数(形参)时,不会影响到实际参数。
基本数据类型的值传递
测试类
1 public class TestClass { 2 public static void test(int value) { 3 value = 2; 4 System.out.println("形参value的值:" + value); 5 } 6 ? 7 public static void main(String[] args) { 8 int value = 1; 9 System.out.println("调用函数前value的值:" + value); 10 test(value); 11 System.out.println("调用函数后value的值:" + value); 12 } 13 }
结果为
1 调用函数前value的值:1 2 形参value的值:2 3 调用函数后value的值:1
结论:可以看到,int类型的value初始为1,调用函数后,value仍然为1,基本数据类型在函数中修改参数(形参)时不会影响到实参的值。
引用数据类型的值传递
类TreeNode
1 public class TreeNode { 2 int val; 3 TreeNode left; 4 TreeNode right; 5 ? 6 TreeNode() { 7 } 8 ? 9 TreeNode(int val) { 10 this.val = val; 11 } 12 ? 13 TreeNode(int val, TreeNode left, TreeNode right) { 14 this.val = val; 15 this.left = left; 16 this.right = right; 17 } 18 }
测试类1
1 public class TestClass { 2 public static void test(TreeNode node) { 3 node.val = 2; 4 System.out.println("形参node的val值:" + node.val); 5 } 6 ? 7 public static void main(String[] args) { 8 TreeNode node = new TreeNode(1); 9 System.out.println("调用函数前node的val值:" + node.val); 10 test(node); 11 System.out.println("调用函数后node的val值:" + node.val); 12 } 13 }
结果为
1 调用函数前node的val值:1 2 形参node的val值:2 3 调用函数后node的val值:2
结论:可以看到,TreeNode类型的node对象的val值初始为1,调用函数后