From 6355230772b581df40c1c3d6356623228667d906 Mon Sep 17 00:00:00 2001 From: Anirudh Venkateshwaran Date: Sun, 17 May 2026 17:14:05 -0700 Subject: [PATCH] Solved Path Sum II and Symmetric Tree --- Problem1.cs | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Problem2.cs | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 Problem1.cs create mode 100644 Problem2.cs diff --git a/Problem1.cs b/Problem1.cs new file mode 100644 index 00000000..2b5d95b8 --- /dev/null +++ b/Problem1.cs @@ -0,0 +1,66 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + + +// Your code here along with comments explaining your approach + +/* + I perform dfs on the tree. I maintain a global variable result which is a list of list of integer. I also maintain sumSoFar and temp list of integer as variables of the recursive function. + In the helper function, I return if root is null. If it's a leaf node, I check if the sumSoFar = target sum, if so I add the list formed to the final resultant list. I backtrack by removing the + last element of the temp list after recursive calls on both the left child and right child has been performed. +*/ + +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +public class Solution +{ + private List> result; + public IList> PathSum(TreeNode root, int targetSum) + { + result = new(); + + if (root == null) + { + return result; + } + + Helper(root, targetSum, 0, new List()); + return result; + } + + public void Helper(TreeNode root, int targetSum, int sumSoFar, List temp) + { + if (root == null) + { + return; + } + + sumSoFar += root.val; + temp.Add(root.val); + + if (root.left == null && root.right == null) + { + if (sumSoFar == targetSum) + result.Add(new List(temp)); + + } + + Helper(root.left, targetSum, sumSoFar, temp); + Helper(root.right, targetSum, sumSoFar, temp); + + temp.RemoveAt(temp.Count - 1); + } +} \ No newline at end of file diff --git a/Problem2.cs b/Problem2.cs new file mode 100644 index 00000000..45f61942 --- /dev/null +++ b/Problem2.cs @@ -0,0 +1,58 @@ +// Time Complexity : O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + + +// Your code here along with comments explaining your approach + +/* +I perform DFS on the left and right child of the root node. In the helper function, if either of the nodes are null I return false. If both are null, I return true. If the +value of the first node is not equal to that of the second node, I return false. Then I recursively perform the check on the left child of the first node and right child of the +second node and the right child of the first node and left child of the second node. + +*/ + +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +public class Solution +{ + public bool IsSymmetric(TreeNode root) + { + return Helper(root.left, root.right); + } + + public bool Helper(TreeNode a, TreeNode b) + { + if (a == null || b == null) + { + if (a == null && b == null) + { + return true; + } + + else + { + return false; + } + } + + if (a.val != b.val) + { + return false; + } + + return Helper(a.left, b.right) && Helper(a.right, b.left); + } +} \ No newline at end of file