From 91924f2b02dca8360b807e1c7a089fde23f2b1a3 Mon Sep 17 00:00:00 2001 From: tmujje Date: Fri, 14 May 2021 01:15:12 -0400 Subject: [PATCH 1/2] Trees-3 Submission --- PathSum2.java | 35 +++++++++++++++++++++++++++++++++++ SymmetricTree.java | 26 ++++++++++++++++++++++++++ 2 files changed, 61 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..b3bce635 --- /dev/null +++ b/PathSum2.java @@ -0,0 +1,35 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Time Complexity : O(n) since we are traversing across all the nodes in the tree in worst case scenario +//Space Complexity: O(1) since we are not taking any extra space + +class Solution { + + List> resultList = new ArrayList<>(); + + public List> pathSum(TreeNode root, int targetSum) + { + return helper(root, 0, targetSum, new ArrayList<>()); + } + + private List> helper(TreeNode root, int rSum, int targetSum, List path) + { + //Base Condition + if(root == null) return new ArrayList<>(); + + path.add(root.val); + rSum = rSum + root.val; + if(root.left == null && root.right == null && rSum == targetSum){ + resultList.add(new ArrayList(path)); + } + + helper(root.left, rSum, targetSum, path); + helper(root.right, rSum, targetSum, path); + + //Backtrack + path.remove(path.size() - 1); + + return resultList; + } +} \ No newline at end of file diff --git a/SymmetricTree.java b/SymmetricTree.java new file mode 100644 index 00000000..86b3d70a --- /dev/null +++ b/SymmetricTree.java @@ -0,0 +1,26 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Time Complexity : O(n) since we are through all nodes of the tree. +//Space Complexity: O(1) since we are not taking any extra space +class Solution { + public boolean isSymmetric(TreeNode root) + { + if(root == null) return false; + return helper(root.left, root.right); + } + + private boolean helper(TreeNode left, TreeNode right) + { + //base condition + if(left == null && right == null) return true; + + //If any of the left or right nodes is null, that means the other part is not null and so not symmetric + if(left == null || right == null) return false; + + //logic + if(left.val != right.val) return false; + + return helper(left.left, right.right) && helper(left.right, right.left); + } +} \ No newline at end of file From 6db978044bac796ed12d249601679b381d095fd4 Mon Sep 17 00:00:00 2001 From: TejBharath Mujje Date: Fri, 21 Aug 2026 00:18:11 -0400 Subject: [PATCH 2/2] Complete Trees-3 assignment --- PathSum2.java | 35 ------------------------- PathSumTwo.java | 35 +++++++++++++++++++++++++ SymmetricTree.java => SymetricTree.java | 16 +++++------ 3 files changed, 42 insertions(+), 44 deletions(-) delete mode 100644 PathSum2.java create mode 100644 PathSumTwo.java rename SymmetricTree.java => SymetricTree.java (66%) diff --git a/PathSum2.java b/PathSum2.java deleted file mode 100644 index b3bce635..00000000 --- a/PathSum2.java +++ /dev/null @@ -1,35 +0,0 @@ -// Did this code successfully run on Leetcode : Yes -// Any problem you faced while coding this : No - -//Time Complexity : O(n) since we are traversing across all the nodes in the tree in worst case scenario -//Space Complexity: O(1) since we are not taking any extra space - -class Solution { - - List> resultList = new ArrayList<>(); - - public List> pathSum(TreeNode root, int targetSum) - { - return helper(root, 0, targetSum, new ArrayList<>()); - } - - private List> helper(TreeNode root, int rSum, int targetSum, List path) - { - //Base Condition - if(root == null) return new ArrayList<>(); - - path.add(root.val); - rSum = rSum + root.val; - if(root.left == null && root.right == null && rSum == targetSum){ - resultList.add(new ArrayList(path)); - } - - helper(root.left, rSum, targetSum, path); - helper(root.right, rSum, targetSum, path); - - //Backtrack - path.remove(path.size() - 1); - - return resultList; - } -} \ No newline at end of file diff --git a/PathSumTwo.java b/PathSumTwo.java new file mode 100644 index 00000000..007e2c21 --- /dev/null +++ b/PathSumTwo.java @@ -0,0 +1,35 @@ +//Time Complexity - O(n) +//Space Complexity - O(h) +class Solution { + List> result = new ArrayList<>(); + public List> pathSum(TreeNode root, int targetSum) + { + //Validate the root + if (root == null) return new ArrayList<>(); + return helper(root, 0, targetSum, new ArrayList<>()); + } + + public List> helper(TreeNode root, int currSum, int targetSum, List path) + { + //base case + if (root == null) return new ArrayList<>(); + + //logic + path.add(root.val); + currSum += root.val; + + if (root.left == null && root.right == null && currSum == targetSum) + { + result.add(new ArrayList(path)); + } + + //recurse + helper(root.left, currSum, targetSum, path); + helper(root.right, currSum, targetSum, path); + + //back track + path.remove(path.size() - 1); + + return result; + } +} \ No newline at end of file diff --git a/SymmetricTree.java b/SymetricTree.java similarity index 66% rename from SymmetricTree.java rename to SymetricTree.java index 86b3d70a..dea0e200 100644 --- a/SymmetricTree.java +++ b/SymetricTree.java @@ -1,9 +1,7 @@ -// Did this code successfully run on Leetcode : Yes -// Any problem you faced while coding this : No - -//Time Complexity : O(n) since we are through all nodes of the tree. -//Space Complexity: O(1) since we are not taking any extra space -class Solution { +//Approach: Using recursion we can check if the left value is not equal to right val, then return else or else the left and right subtrees are symmetric. +//Time Complexity : O(n) +//Space Complexity: O(1) +class SymetricTree { public boolean isSymmetric(TreeNode root) { if(root == null) return false; @@ -17,10 +15,10 @@ private boolean helper(TreeNode left, TreeNode right) //If any of the left or right nodes is null, that means the other part is not null and so not symmetric if(left == null || right == null) return false; - - //logic + + //left value when equals to right value then it is considered symetric if(left.val != right.val) return false; - + return helper(left.left, right.right) && helper(left.right, right.left); } } \ No newline at end of file