Instructions:
Solution:
//https://www.codewars.com/kata/57e5279b7cf1aea5cf000359/train/javaimport java.util.ArrayList;class Solution{ static int maxSum(TreeNode root){ ArrayList> allPaths = new FindShortestBTPath().findAllPath(root); int max = 0; for(ArrayList path: paths){ int sum = 0; for(Integer x : path){ sum += x; } if(sum > max){ max = sum; } } return max; } private static class FindShortestBTPath{ private ArrayList > allPaths = new ArrayList >(); private ArrayList onePath = ArrayList (); public ArrayList > findAllPath(TreeNode root){ if(root == null){ return allPaths; } onePath.add(root.value); if(root.left == null && root.right == null){ allPaths.add(newArrayList (onePath)); } findAllPath(root.left); findAllPath(root.right); onePath.remove(onePath.size() - 1); return allPaths; } } }
Example Test:
https://www.codewars.com/kata/57e5279b7cf1aea5cf000359/train/java