subject
Input the following node and an integer of a binary tree, and print out all paths in which the sum of node values in the binary tree is the input integer. Path is defined as a path from the root node of the tree to the node that the leaf node passes through. (Note: in the list of returned values, the array with large array length is first)
thinking
It can be judged by traversing the root node one by one.
Note: copy operation with linked list after code
import java.util.ArrayList; /** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) { ArrayList<ArrayList<Integer>> ans = new ArrayList<ArrayList<Integer>>(); if(root == null) return ans; ArrayList<Integer> list = new ArrayList<Integer>(); Find(root, target, ans, list, 0); return ans; } public void Find(TreeNode root, int target, ArrayList<ArrayList<Integer>> ans, ArrayList<Integer> list, int sum){ if(root == null || sum+root.val > target) return ; sum += root.val; if(root.left == null && root.right == null){ if(target == sum){ list.add(root.val); ans.add(new ArrayList<Integer>(list));//Add the same list as list list.remove(list.size()-1);//Remove the newly added node for subsequent operations in other situations } return ; } list.add(root.val); //Cannot be placed before the previous if Find(root.left, target, ans, list, sum); Find(root.right, target, ans, list, sum); list.remove(list.size()-1);//Remove the newly added node for subsequent operations in other situations } }
Linked list replication
Two species:
â‘ ArrayList<Integer> desc = new ArrayList<Integer>(src);
â‘¡Collections.copy(desc, src);
In the way of a = b, one linked list operation will affect the other linked list data
It's not very clear about the deep copy and shallow copy involved. If there is a way to practice and prove it, can you tell me?
import java.util.ArrayList; import java.util.Collections; public class Solution { public static void main(String[] args) { System.out.println("The following operations on a single linked list do not affect the data of another linked list(Operations with comments)"); System.out.println("==================ArrayList<Integer> desc = new ArrayList<Integer>(src);==============================="); ArrayList<Integer> a = new ArrayList<Integer>(); a.add(4); a.add(5); a.add(65); for (Integer integer : a) { System.out.print(integer+" "); } System.out.println(); ArrayList<Integer> b = new ArrayList<Integer>(a); for (Integer integer : b) { System.out.print(integer+" "); } System.out.println(); System.out.println("Comparison:"+(a==b)); //b.clear(); a.set(0, 1000); //b.remove(2); for (Integer integer : a) { System.out.print(integer+" "); } System.out.println(); for (Integer integer : b) { System.out.print(integer+" "); } System.out.println("\n====================Collections.copy(desc, src);============================="); ArrayList<Integer> c = new ArrayList<Integer>(); c.add(54); c.add(65); c.add(87); Collections.copy(c, b); System.out.println("Comparison:"+(c==b)); for (Integer integer : b) { System.out.print(integer+" "); } System.out.println(); for (Integer integer : c) { System.out.print(integer+" "); } System.out.println(); //b.clear(); //c.set(0, 1000); b.remove(2); for (Integer integer : c) { System.out.print(integer+" "); } System.out.println(); for (Integer integer : b) { System.out.print(integer+" "); } } }
Operation result:
If there is any mistake or unreasonable place, please correct it~
Come on!!