An input binary tree root node and an integer, the binary print values of nodes in the input path and all integers. Forming a path to the path definition begins from the root node of the tree down to the leaf node has been traversed nodes. (Note: the return value in the list, a large array Array front)
A preorder traversal of the tree to record the path
public class Solution { ArrayList<ArrayList<Integer>> paths=new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> path=new ArrayList<Integer>();//存储当前的路径 public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) { if(target < 0){ return paths; } if(root == null){ return paths; } path.add(root.val); if(root.left == null &&root.right==null&& target == root.val){ paths.add(new ArrayList<Integer>(path)); } if(root.val < target && root.left!=null){ FindPath(root.left,target-root.val); } if(root.val < target&& root.left!=null){ FindPath(root.right,target-root.val); }
//遍历完一条路径之后需要回溯 path.remove(path.size()-1); return paths; } }
Reference blog: https://blog.csdn.net/u014525494/article/details/80978647
After only fully understood in view of a part of the recording, etc.