Skip to content

Commit

Permalink
Added codes for 14 April
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Apr 14, 2024
1 parent 2e8f603 commit 6953d24
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
53 changes: 53 additions & 0 deletions GeeksForGeeks/April/14-4-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//{ Driver Code Starts
import java.util.*;

public class GFG {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
Solution obj = new Solution();
obj.xor1ToN(n, arr);
obj.printArr(n, arr);
obj.setToZero(n, arr);
obj.printArr(n, arr);
}
sc.close();
}
}

// } Driver Code Ends


// User function Template for Java
class Solution
{
public void printArr(int n, int arr[])
{
// code here
for(int a:arr)
System.out.print(a+" ");

System.out.println();
}

public void setToZero(int n, int arr[])
{
// code here
Arrays.fill(arr,0);
}

public void xor1ToN(int n, int arr[])
{
// code here
for(int i=0;i<n;i++)
{
arr[i]^=i;
}
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/April/14-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/April/14-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(h)
37 changes: 37 additions & 0 deletions LeetCode/April/14-4-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 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
{
public int sumOfLeftLeaves(TreeNode root)
{
if (root == null)
return 0;

int ans = 0;

if (root.left != null)
{
if (root.left.left == null && root.left.right == null)
ans += root.left.val;
else
ans += sumOfLeftLeaves(root.left);
}

ans += sumOfLeftLeaves(root.right);

return ans;
}
}

0 comments on commit 6953d24

Please sign in to comment.