Skip to content
Open
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
51 changes: 51 additions & 0 deletions pathsum2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Time Complexity : O(n)
// Space Complexity : O(n) - for the recursion stack
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : local and global variables were not being updated correctly, which was giving wrong results. After fixing that, it worked fine.

Comment on lines +1 to +5
// Your code here along with comments explaining your approach: base case is when the root is null, return. Add the current node value to the current sum and add it to the path. If the current node is a leaf node, check if the current sum equals the target sum. If it does, add the current path to the result list. Recurse on the left and right subtrees. After returning from recursion, backtrack by removing the last node from the path.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
List<List<Integer>> result;
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
this.result = new ArrayList<>();
helper(root, targetSum, 0, new ArrayList<>());
return result;
Comment on lines +24 to +27
}

private void helper(TreeNode root, int targetSum, int currSum, List<Integer> path){
// base case
if(root == null) return;

//action
currSum += root.val;
path.add(root.val);

if(root.left == null && root.right == null){
if(currSum == targetSum){
result.add(new ArrayList<>(path));
}
}

// recurse
helper(root.left, targetSum, currSum, path);
helper(root.right, targetSum, currSum, path);

// backtrack
path.remove(path.size()-1);
}
}
51 changes: 51 additions & 0 deletions symmetrictree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Time Complexity : O(n)
// Space Complexity : O(n) - for the recursion stack
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : yes writing logic for backtracking and keeping track of the current sum and path

Comment on lines +1 to +5
// Your code here along with comments explaining your approach: traverse the tree in a depth-first manner, keeping track of the current sum and path from root to leaf. When a leaf node is reached, check if the current sum equals the target sum. If it does, add the current path to the result list. Use backtracking to remove the last node from the path when returning from recursion.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
List<List<Integer>> result;
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
this.result = new ArrayList<>();
helper(root, targetSum, 0, new ArrayList<>());
return result;
Comment on lines +24 to +27
Comment on lines +24 to +27
}

private void helper(TreeNode root, int targetSum, int currSum, List<Integer> path){
// base case
if(root == null) return;

//action
currSum += root.val;
path.add(root.val);

if(root.left == null && root.right == null){
if(currSum == targetSum){
result.add(new ArrayList<>(path));
}
}

// recurse
helper(root.left, targetSum, currSum, path);
helper(root.right, targetSum, currSum, path);

// backtrack
path.remove(path.size()-1);
}
}