diff --git a/PathSum2.java b/PathSum2.java new file mode 100644 index 00000000..4788ba7a --- /dev/null +++ b/PathSum2.java @@ -0,0 +1,49 @@ +import java.util.*; +/** + * 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 { + // SC = O(h) + // TC = O(nh) + /* + This solution uses DFS with backtracking to find all root-to-leaf paths whose sum equals the target. + Starting from the root, each visited node is added to the current path (list), and its value is subtracted from targetSum. + If a leaf node is reached and the remaining targetSum is 0, the current path is a valid answer and is added to the result. + The recursion continues by exploring both the left and right subtrees. + After exploring a node's children, the node is removed from the current path (backtracking) so the same list can be reused for other paths. + */ + List> result = new ArrayList<>(); + public List> pathSum(TreeNode root, int targetSum) { + dfs(root, targetSum, new ArrayList()); + return result; + } + + private void dfs(TreeNode root, int targetSum, List list) { + if (root == null) { + return; + } + + targetSum -= root.val; + list.add(root.val); + + if (targetSum == 0 && root.left == null && root.right == null) { + result.add(new ArrayList<>(list)); + } + + dfs(root.left, targetSum, list); + dfs(root.right, targetSum, list); + list.remove(list.size() - 1); + } +} \ No newline at end of file diff --git a/SymmetricTree.java b/SymmetricTree.java new file mode 100644 index 00000000..21be7f19 --- /dev/null +++ b/SymmetricTree.java @@ -0,0 +1,49 @@ +import java.util.*; + +/** + * 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 { + /* + TC - O(n) + SC - O(n) + We use BFS for this solution (level-order traversal with a queue). + + We start by adding the root twice into a queue to compare the tree with itself in a mirrored way. + In each step, two nodes (t1 and t2) are taken from the queue and compared: + If both are null, continue (they match). If only one is null, or their values are different, the tree is not symmetric. + If values match, their children are added and compared in the following order :- + t1.left with t2.right + t1.right with t2.left + This ensures we always compare opposite sides of the tree. If all pairs match, the tree is symmetric. + */ + public boolean isSymmetric(TreeNode root) { + Queue q = new LinkedList<>(); + q.add(root); + q.add(root); + while (!q.isEmpty()) { + TreeNode t1 = q.poll(); + TreeNode t2 = q.poll(); + if (t1 == null && t2 == null) continue; + if (t1 == null || t2 == null) return false; + if (t1.val != t2.val) return false; + q.add(t1.left); + q.add(t2.right); + q.add(t1.right); + q.add(t2.left); + } + return true; + } +} \ No newline at end of file