Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Tree, Backtracking, Depth-First Search, Binary Tree
// Tags: Tree, DFS, Backtracking
package com.leetcode.medium;

import com.leetcode.datastructure.TreeNode;

import java.util.List;
import java.util.ArrayList;
import java.util.List;

public class PathSumII113 {
private void dfs(TreeNode node, List<List<Integer>> res, List<Integer> path, int remainingSum) {
Expand All @@ -14,8 +17,9 @@ private void dfs(TreeNode node, List<List<Integer>> res, List<Integer> path, int
}
this.dfs(node.left, res, path, remainingSum - node.val);
this.dfs(node.right, res, path, remainingSum - node.val);
path.remove(path.size() - 1);
path.removeLast();
}

/*
Time Complexity: O(N^2)
where N are the number of nodes in a tree.
Expand All @@ -29,14 +33,7 @@ Hence, the complexity in the worst case could be O(N^2).
*/
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
this.dfs(root, res, path, targetSum);
this.dfs(root, res, new ArrayList<>(), targetSum);
return res;
}

public static void main(String[] args) {
TreeNode root = new TreeNode(5, new TreeNode(4, new TreeNode(11, new TreeNode(7), new TreeNode(2)), null), new TreeNode(8, new TreeNode(13), new TreeNode(4, new TreeNode(5), new TreeNode(1))));
int targetSum = 22;
System.out.println(new PathSumII113().pathSum(root, targetSum)); // [[5, 4, 11, 2], [5, 8, 4, 5]]
}
}
86 changes: 86 additions & 0 deletions src/test/java/com/leetcode/medium/PathSumII113Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.leetcode.medium;

import com.leetcode.datastructure.TreeNode;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class PathSumII113Test {
private PathSumII113 solution;

@BeforeEach
public void setUp() {
solution = new PathSumII113();
}

@Test
void testPathSum_Example1() {
TreeNode root = new TreeNode(5);
root.left = new TreeNode(4);
root.right = new TreeNode(8);
root.left.left = new TreeNode(11);
root.left.left.left = new TreeNode(7);
root.left.left.right = new TreeNode(2);
root.right.left = new TreeNode(13);
root.right.right = new TreeNode(4);
root.right.right.left = new TreeNode(5);
root.right.right.right = new TreeNode(1);

int targetSum = 22;

List<List<Integer>> expected = List.of(
List.of(5, 4, 11, 2),
List.of(5, 8, 4, 5)
);

List<List<Integer>> result = solution.pathSum(root, targetSum);

assertEquals(expected, result);
}

@Test
void testPathSum_Example2() {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);

int targetSum = 5;

List<List<Integer>> expected = List.of();

List<List<Integer>> result = solution.pathSum(root, targetSum);

assertEquals(expected, result);
}

@Test
void testPathSum_Example3() {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);

int targetSum = 0;

List<List<Integer>> expected = List.of();

List<List<Integer>> result = solution.pathSum(root, targetSum);

assertEquals(expected, result);
}

@Test
void testPathSum_Example4() {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);

int targetSum = 1;

List<List<Integer>> expected = List.of();

List<List<Integer>> result = solution.pathSum(root, targetSum);

assertEquals(expected, result);
}
}