diff --git a/Mirrortree.java b/Mirrortree.java new file mode 100644 index 00000000..0651ee0c --- /dev/null +++ b/Mirrortree.java @@ -0,0 +1,52 @@ +// Time Complexity :O(n) +// Space Complexity :O(h) where h is the height of the tree +// Did this code successfully run on Leetcode :Yes +// Any problem you faced while coding this : +// Confusion the traversal order to be used for left and right subtree. +// I was using the same order for both the subtrees which was giving wrong answer. +// After understanding the traversal order, I was able to solve the problem. + +// Your code here along with comments explaining your approach +// We will be doing a pre-order traversal for both the left and right subtree. +// For left subtree, we will be traversing in the order of root->left->right +// For right subtree, we will be traversing in the order of root->right->left +// We will be comparing the values of the nodes at each level. +// If they are not same, we will return false. +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 { + + boolean res; + + public boolean isSymmetric(TreeNode root) { + + res = true; + helper(root.left, root.right); + return res; + } + + private void helper(TreeNode left, TreeNode right) { + + if (left == null && right == null) { + return; + } + else if (left == null || right == null || left.val != right.val) { + res = false; + return; + } + + helper(left.left, right.right); + helper(left.right, right.left); + } +} \ No newline at end of file diff --git a/PathSum2.java b/PathSum2.java new file mode 100644 index 00000000..3987e12e --- /dev/null +++ b/PathSum2.java @@ -0,0 +1,46 @@ +// Time Complexity : O(n) where n is the number of nodes in the tree. We are visiting each node once. +// Space Complexity : O(h) where h is the height of the tree. In the worst case, the tree is skewed and the height is n, so the space complexity is O(n). In the best case, the tree is balanced and the height is log(n), so the space complexity is O(log(n)). +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No, I did not face any problem while coding this. + + +// Your code here along with comments explaining your approach +// We will be doing a pre-order traversal of the tree. +// We will be keeping track of the current path and the sum of the values in the current path. +// When we reach a leaf node, we will check if the sum of the values in the current path is equal to the target sum. +// If it is, we will add the current path to the result list. Note we need to make a copy of the current path before adding it to the result list, +// because we will be backtracking and modifying the current path. +// After checking the leaf node, we will backtrack and remove the last node from the current path and subtract its value from the sum. +import java.util.*; + +class Solution { + + List> res; + + public List> pathSum(TreeNode root, int targetSum) { + + res = new ArrayList<>(); + helper(root, targetSum, new ArrayList<>(), 0); + return res; + } + + private void helper(TreeNode root, int targetSum, List path, int sum) { + + if (root == null) return; + + // include current node + sum += root.val; + path.add(root.val); + + // check leaf + if (root.left == null && root.right == null && sum == targetSum) { + res.add(new ArrayList<>(path)); // copy path + } + + helper(root.left, targetSum, path, sum); + helper(root.right, targetSum, path, sum); + + // backtrack + path.remove(path.size() - 1); + } +} \ No newline at end of file