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
31 changes: 30 additions & 1 deletion Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,33 @@
// Any problem you faced while coding this :


// Your code here along with comments explaining your approach
// Your code here along with comments explaining your approach

public class Sample {
// Time Complexity : O(n)
// Space Complexity : O(h)
// Did this code successfully run on Leetcode : yes
List<List<Integer>> ans = new ArrayList<>();
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
int currSum = 0;

helper(root, currSum, new ArrayList<>(), targetSum);
return ans;
}

public void helper(TreeNode root, int currSum, List<Integer> list, int tar) {
if (root == null) return;
currSum = currSum + root.val;
System.out.println(root.val + " " + currSum + " " + tar);

list.add(root.val);
if (root.left == null && root.right == null && tar == currSum) {
ans.add(new ArrayList(list));
} else {
helper(root.left, currSum, list, tar);
helper(root.right, currSum, list, tar);
}
list.remove(list.size()-1);
}

}
17 changes: 17 additions & 0 deletions SymmetricTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class SymmetricTree {
// Time Complexity : O(n)
// Space Complexity : O(h)
// Did this code successfully run on Leetcode : yes
public boolean isSymmetric(TreeNode root) {
if (root == null) return true;
return helper(root.left, root.right);
}

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

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