From 1ab36bab0663304b3e110564536d60836629ae87 Mon Sep 17 00:00:00 2001 From: Praniksha123 Date: Thu, 2 Jul 2026 21:34:45 +0530 Subject: [PATCH] Create trees-3.java --- trees-3.java | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 trees-3.java diff --git a/trees-3.java b/trees-3.java new file mode 100644 index 00000000..568f3caf --- /dev/null +++ b/trees-3.java @@ -0,0 +1,57 @@ +// Approach: +// 1. Perform a DFS traversal while maintaining the current path and its sum. +// 2. Add the current node's value to the path and update the running sum. +// 3. If the current node is a leaf and the running sum equals the target sum, +// add a copy of the current path to the result. +// 4. Recursively explore the left and right subtrees. +// 5. After visiting both subtrees, remove the current node from the path +// (backtracking) to explore other paths. + +// Time Complexity: O(n) +// Space Complexity: O(h) +// h = height of the tree (excluding the output list) +class Solution { + List> res; + public List> pathSum(TreeNode root, int targetSum) { + this.res=new ArrayList<>(); + helper(root,targetSum,0,new ArrayList<>()); + return res; + } + private void helper(TreeNode root,int targetSum,int currentsum,List path){ + if(root==null) return; + currentsum+=root.val; + path.add(root.val); + if(root.left==null && root.right==null){ + if(currentsum==targetSum){ + res.add(new ArrayList<>(path)); + } + } + helper(root.left,targetSum,currentsum,path); + helper(root.right,targetSum,currentsum,path); + path.remove(path.size()-1); + } +} +//problem-2 +// Approach: +// 1. Compare the left and right subtrees recursively. +// 2. If both nodes are null, they are symmetric. +// 3. If one node is null or their values differ, return false. +// 4. Compare the left child of the left subtree with the right child of the right subtree. +// 5. Compare the right child of the left subtree with the left child of the right subtree. +// 6. The tree is symmetric only if both recursive comparisons return true. + +// Time Complexity: O(n) +// Space Complexity: O(h) +// h = height of the tree (recursion stack) +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); + } +}