diff --git a/Problem47.java b/Problem47.java new file mode 100644 index 00000000..35d045f0 --- /dev/null +++ b/Problem47.java @@ -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> result; + public List> 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 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); + } +} \ No newline at end of file diff --git a/Problem48.java b/Problem48.java new file mode 100644 index 00000000..1a2ec176 --- /dev/null +++ b/Problem48.java @@ -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); + } +} \ No newline at end of file diff --git a/Sample.java b/Sample.java deleted file mode 100644 index f5c45b5f..00000000 --- a/Sample.java +++ /dev/null @@ -1,7 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : - - -// Your code here along with comments explaining your approach \ No newline at end of file