diff --git a/PathSum2.java b/PathSum2.java new file mode 100644 index 00000000..f6a8fa4b --- /dev/null +++ b/PathSum2.java @@ -0,0 +1,59 @@ +// Time Complexity : O(n) +// Space Complexity : O(H) , height of the tree +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + +// Your code here along with comments explaining your approach +/* +Create a helper function which would keep track of currentsum by adding root's value in each recursion +and also keeping track of the visited values through a list and recurse the same process for both left and +right children. We will only add the visited path list to our result on the case of leaf nodes and when +target sum is met. We also need to traverse through right branch after we hit leaf node of a specific root, +so, we need to backtrack by 1 position so we can compute that. + */ +/** + * 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> 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) { + 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); + + } +} \ No newline at end of file diff --git a/SymmetricTree.java b/SymmetricTree.java new file mode 100644 index 00000000..5c78d1fb --- /dev/null +++ b/SymmetricTree.java @@ -0,0 +1,41 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + +// Your code here along with comments explaining your approach +/* +Have a recursive function where we try to compare both the left and right branches of the tree along with +their values and also the right and left branches as well to check if they are mirror images to each other +or not. If we are able to traverse both of them until leaf nodes at the same time, it means both are of +same height, if not, we return false. + */ +/** + * 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 { + public boolean isSymmetric(TreeNode root) { + return isMirror(root, root); + } + + private boolean isMirror(TreeNode t1, TreeNode t2) { + if(t1 == null && t2 == null) + return true; + if(t1 == null || t2 == null) + return false; + return ((t1.val == t2.val) && isMirror(t1.left, t2.right) && isMirror(t1.right, t2.left)); + + } +} \ No newline at end of file