From 1762a8555584929adcfbff930fc3f4ed9417a27c Mon Sep 17 00:00:00 2001 From: yashhh-23 Date: Wed, 8 Jul 2026 17:07:10 +0530 Subject: [PATCH] completed trees - 3 --- pathsum2.java | 51 ++++++++++++++++++++++++++++++++++++++++++++++ symmetrictree.java | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 pathsum2.java create mode 100644 symmetrictree.java diff --git a/pathsum2.java b/pathsum2.java new file mode 100644 index 00000000..52d882d0 --- /dev/null +++ b/pathsum2.java @@ -0,0 +1,51 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) - for the recursion stack +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : local and global variables were not being updated correctly, which was giving wrong results. After fixing that, it worked fine. + +// Your code here along with comments explaining your approach: base case is when the root is null, return. Add the current node value to the current sum and add it to the path. If the current node is a leaf node, check if the current sum equals the target sum. If it does, add the current path to the result list. Recurse on the left and right subtrees. After returning from recursion, backtrack by removing the last node from the path. +/** + * 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){ + // base case + 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); + } +} diff --git a/symmetrictree.java b/symmetrictree.java new file mode 100644 index 00000000..1dc36112 --- /dev/null +++ b/symmetrictree.java @@ -0,0 +1,51 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) - for the recursion stack +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : yes writing logic for backtracking and keeping track of the current sum and path + +// Your code here along with comments explaining your approach: traverse the tree in a depth-first manner, keeping track of the current sum and path from root to leaf. When a leaf node is reached, check if the current sum equals the target sum. If it does, add the current path to the result list. Use backtracking to remove the last node from the path when returning from recursion. +/** + * 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){ + // base case + 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); + } +}