commit 37ee7d5b44d16b5134a6b64ef9f8a0f6faab595f parent c55e4f0dee76504429d5eed88abe6eab704e719a Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Sat, 20 May 2023 23:54:41 -0500 Completed path sum ii using dart Diffstat:
| A | path-sum-ii/path-sum-ii.dart | | | 23 | +++++++++++++++++++++++ |
1 file changed, 23 insertions(+), 0 deletions(-)
diff --git a/path-sum-ii/path-sum-ii.dart b/path-sum-ii/path-sum-ii.dart @@ -0,0 +1,23 @@ +//Given a binary tree return every path that adds up to the target sum. +//To do this I recursively called the function for branch to see if from root to leaf +//added up to the target sum if it did I returned that value back to the parent each time adding +//on the current value to the list. +//Time: 292ms Beats: 77.78% +//Memory: 143.7MB Beats: 77.78% + +class Solution { + List<List<int>> pathSum(TreeNode? root, int targetSum) { + if(root == null){ + return []; + } + if(targetSum == (root.val) && root.left == null && root.right == null){ + return [[root.val]]; + } + List<List<int>> list = pathSum(root.left, targetSum - root.val); + list += pathSum(root.right, targetSum - root.val); + for(int i = 0 ; i < list.length ; ++i){ + list[i].insert(0,root.val); + } + return list; + } +}