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
35 changes: 35 additions & 0 deletions PathSumTwo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//Time Complexity - O(n)
//Space Complexity - O(h)
class Solution {
List<List<Integer>> result = new ArrayList<>();
public List<List<Integer>> pathSum(TreeNode root, int targetSum)
{
//Validate the root
if (root == null) return new ArrayList<>();
return helper(root, 0, targetSum, new ArrayList<>());
}

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

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

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

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

//back track
path.remove(path.size() - 1);

return result;
}
}
24 changes: 24 additions & 0 deletions SymetricTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//Approach: Using recursion we can check if the left value is not equal to right val, then return else or else the left and right subtrees are symmetric.
//Time Complexity : O(n)
//Space Complexity: O(1)
class SymetricTree {
public boolean isSymmetric(TreeNode root)
{
if(root == null) return false;
return helper(root.left, root.right);
}

private boolean helper(TreeNode left, TreeNode right)
{
//base condition
if(left == null && right == null) return true;

//If any of the left or right nodes is null, that means the other part is not null and so not symmetric
if(left == null || right == null) return false;

//left value when equals to right value then it is considered symetric
if(left.val != right.val) return false;

return helper(left.left, right.right) && helper(left.right, right.left);
}
}