From bf7806fe97462abf2aafbe02250fdff76752d8b8 Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Sun, 5 May 2024 09:05:37 +0530 Subject: [PATCH] Added codes for 5 May --- GeeksForGeeks/May/05-5-24/GFG.java | 144 ++++++++++++++++++++++++++++ GeeksForGeeks/May/05-5-24/README.md | 2 + LeetCode/May/05-5-24/README.md | 2 + LeetCode/May/05-5-24/Solution.java | 14 +++ 4 files changed, 162 insertions(+) create mode 100644 GeeksForGeeks/May/05-5-24/GFG.java create mode 100644 GeeksForGeeks/May/05-5-24/README.md create mode 100644 LeetCode/May/05-5-24/README.md create mode 100644 LeetCode/May/05-5-24/Solution.java diff --git a/GeeksForGeeks/May/05-5-24/GFG.java b/GeeksForGeeks/May/05-5-24/GFG.java new file mode 100644 index 0000000..c1a02a4 --- /dev/null +++ b/GeeksForGeeks/May/05-5-24/GFG.java @@ -0,0 +1,144 @@ +//{ Driver Code Starts +//Initial Template for Java + +import java.util.LinkedList; +import java.util.Queue; +import java.io.*; +import java.util.*; + +class Node{ + int data; + Node left; + Node right; + Node(int data){ + this.data = data; + left=null; + right=null; + } +} + +class GfG { + + static Node buildTree(String str){ + + if(str.length()==0 || str.charAt(0)=='N'){ + return null; + } + + String ip[] = str.split(" "); + // Create the root of the tree + Node root = new Node(Integer.parseInt(ip[0])); + // Push the root to the queue + + Queue queue = new LinkedList<>(); + + queue.add(root); + // Starting from the second element + + int i = 1; + while(queue.size()>0 && i < ip.length) { + + // Get and remove the front of the queue + Node currNode = queue.peek(); + queue.remove(); + + // Get the current node's value from the string + String currVal = ip[i]; + + // If the left child is not null + if(!currVal.equals("N")) { + + // Create the left child for the current node + currNode.left = new Node(Integer.parseInt(currVal)); + // Push it to the queue + queue.add(currNode.left); + } + + // For the right child + i++; + if(i >= ip.length) + break; + + currVal = ip[i]; + + // If the right child is not null + if(!currVal.equals("N")) { + + // Create the right child for the current node + currNode.right = new Node(Integer.parseInt(currVal)); + + // Push it to the queue + queue.add(currNode.right); + } + i++; + } + + return root; + } + + public static void main (String[] args) throws IOException{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + int t=Integer.parseInt(br.readLine()); + + while(t > 0){ + String s = br.readLine(); + Node root = buildTree(s); + Solution g = new Solution(); + + ArrayList res = g.verticalSum(root); + for (Integer num : res) System.out.print (num + " "); + System.out.println(); + t--; + + } + } + +} + + +// } Driver Code Ends + + +/*Complete the function below +Node is as follows: +class Node{ +int data; +Node left, right; +Node(int item) +{ + data = item; + left = right = null +} +} +*/ +class Solution +{ + public ArrayList verticalSum(Node root) + { + // add your code here + TreeMap map = new TreeMap<>(); + + helper(root,map,0); + + ArrayList ans = new ArrayList<>(); + + for(int i: map.keySet()) + ans.add(map.get(i)); + + Collections.reverse(ans); + + return ans; + } + + private void helper(Node root, TreeMapmap, int cur) + { + if(root == null) + return; + + map.put(cur, map.getOrDefault(cur,0) + root.data); + + helper(root.left, map,cur+1); + helper(root.right, map,cur-1); + } +} diff --git a/GeeksForGeeks/May/05-5-24/README.md b/GeeksForGeeks/May/05-5-24/README.md new file mode 100644 index 0000000..fbf4193 --- /dev/null +++ b/GeeksForGeeks/May/05-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n * logn) +Space complexity - O(n) diff --git a/LeetCode/May/05-5-24/README.md b/LeetCode/May/05-5-24/README.md new file mode 100644 index 0000000..b85bed6 --- /dev/null +++ b/LeetCode/May/05-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(1) +Space complexity - O(1) diff --git a/LeetCode/May/05-5-24/Solution.java b/LeetCode/May/05-5-24/Solution.java new file mode 100644 index 0000000..4f92a36 --- /dev/null +++ b/LeetCode/May/05-5-24/Solution.java @@ -0,0 +1,14 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +class Solution { + public void deleteNode(ListNode node) { + node.val = node.next.val; + node.next = node.next.next; + } +}