From 3f6edd9229603dbf177ea5a7a2d3861bc94393e1 Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Tue, 7 May 2024 07:27:22 +0530 Subject: [PATCH] Added codes for 7 May --- GeeksForGeeks/May/07-5-24/.Solution.java.swp | Bin 0 -> 1024 bytes GeeksForGeeks/May/07-5-24/GFG.java | 160 +++++++++++++++++++ GeeksForGeeks/May/07-5-24/README.md | 2 + LeetCode/May/07-5-24/README.md | 2 + LeetCode/May/07-5-24/Solution.java | 28 ++++ 5 files changed, 192 insertions(+) create mode 100644 GeeksForGeeks/May/07-5-24/.Solution.java.swp create mode 100644 GeeksForGeeks/May/07-5-24/GFG.java create mode 100644 GeeksForGeeks/May/07-5-24/README.md create mode 100644 LeetCode/May/07-5-24/README.md create mode 100644 LeetCode/May/07-5-24/Solution.java diff --git a/GeeksForGeeks/May/07-5-24/.Solution.java.swp b/GeeksForGeeks/May/07-5-24/.Solution.java.swp new file mode 100644 index 0000000000000000000000000000000000000000..cb486417380a913fafde894b38b47388aafb53fb GIT binary patch literal 1024 zcmYc?$V<%2S1{8vVn6} 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; + } + static void printInorder(Node root) + { + if(root == null) + return; + + printInorder(root.left); + System.out.print(root.data+" "); + + printInorder(root.right); + } + + 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); + Tree g = new Tree(); + ArrayList ans = g.reverseLevelOrder(root); + + for (Integer val: ans) + System.out.print(val+" "); + System.out.println(); + t--; + } + } +} + + + + +// } Driver Code Ends + + + +/* +class Node +{ + int data; + Node left, right; + + Node(int item) + { + data = item; + left = right = null; + } +} +*/ + +class Tree +{ + public ArrayList reverseLevelOrder(Node node) + { + // code here + ArrayList result = new ArrayList<>(); + if (node == null) + return result; + + Queue queue = new LinkedList<>(); + queue.offer(node); + + while (!queue.isEmpty()) + { + int levelSize = queue.size(); + ArrayList levelNodes = new ArrayList<>(); + + for (int i = 0; i < levelSize; i++) + { + Node current = queue.poll(); + levelNodes.add(current.data); + + if (current.left != null) + queue.offer(current.left); + if (current.right != null) + queue.offer(current.right); + } + + result.addAll(0, levelNodes); + } + + return result; + } +} diff --git a/GeeksForGeeks/May/07-5-24/README.md b/GeeksForGeeks/May/07-5-24/README.md new file mode 100644 index 0000000..7384c4b --- /dev/null +++ b/GeeksForGeeks/May/07-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(n) diff --git a/LeetCode/May/07-5-24/README.md b/LeetCode/May/07-5-24/README.md new file mode 100644 index 0000000..9567f26 --- /dev/null +++ b/LeetCode/May/07-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(1) diff --git a/LeetCode/May/07-5-24/Solution.java b/LeetCode/May/07-5-24/Solution.java new file mode 100644 index 0000000..5260031 --- /dev/null +++ b/LeetCode/May/07-5-24/Solution.java @@ -0,0 +1,28 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution +{ + public ListNode doubleIt(ListNode head) + { + if (head.val >= 5) + head = new ListNode(0, head); + + for (ListNode curr = head; curr != null; curr = curr.next) + { + curr.val *= 2; + curr.val %= 10; + if (curr.next != null && curr.next.val >= 5) + ++curr.val; + } + + return head; + } +}