class Solution { public static ArrayList<ArrayList<Integer>> Paths(Node root) { // code here ArrayList<ArrayList<Integer>> result = new ArrayList<>(); pathToLeaf(root,result,new ArrayList<>()); return result; } private static void pathToLeaf(Node node, List<ArrayList<Integer>> result, ArrayList<Integer> sub){ if(node == null){ return; } sub.add(node.data); if(node.left==null && node.right == null){ result.add(new ArrayList<>(sub)); } pathToLeaf(node.left,result,sub); pathToLeaf(node.right,result,sub); sub.remove(sub.size()-1); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter