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
33 changes: 33 additions & 0 deletions Problem47.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Time Complexity : O(N)
// Space Complexity : O(h)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

class Solution {
private 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;
}

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

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

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

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

path.remove(path.size()-1);
}
}
21 changes: 21 additions & 0 deletions Problem48.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Time Complexity : O(N)
// Space Complexity : O(h)

class Solution {
public boolean isSymmetric(TreeNode root) {
if(root == null) return true;
return helper(root.left, root.right);
}

private boolean helper(TreeNode left, TreeNode right){
if(left == null && right == null) return true;
if(left == null || right == null){
return false;
}
if(left.val != right.val){
return false;
}

return helper(left.left, right.right) && helper(left.right, right.left);
}
}
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.