diff --git a/Balanced Binary Tree Question b/Balanced Binary Tree Question new file mode 100644 index 00000000..ae0e539c --- /dev/null +++ b/Balanced Binary Tree Question @@ -0,0 +1,21 @@ +110. Balanced Binary Tree {Easy} + +Given a binary tree, determine if it is +height-balanced. + +Example 1: +Input: root = [3,9,20,null,null,15,7] +Output: true + +Example 2: +Input: root = [1,2,2,3,3,null,null,4,4] +Output: false + +Example 3: +Input: root = [] +Output: true + +Constraints: + +The number of nodes in the tree is in the range [0, 5000]. +-104 <= Node.val <= 104 \ No newline at end of file diff --git a/BalancedBinaryTree.java b/BalancedBinaryTree.java new file mode 100644 index 00000000..f9089e5b --- /dev/null +++ b/BalancedBinaryTree.java @@ -0,0 +1,52 @@ +/* +Time Complexity: +The method height is a recursive function that visits each node once. +At each node: + It recursively computes the height of the left and right subtrees. + It performs constant-time checks (e.g., if conditions, Math.abs, and Math.max). +Thus, the total time complexity is O(n), where n is the number of nodes in the tree. + +Space Complexity: +The space complexity is O(h), where h is the height of the tree: + Worst Case (Skewed Tree): O(n) + Best Case (Balanced Tree): O(logn) + +Did this code successfully run on Leetcode: Yes +*/ + +//Definition for a binary tree node. +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 BalancedBinaryTree { + public boolean isBalanced(TreeNode root) + { + if(root == null) return true; + return height(root) != -1; + } + + private int height(TreeNode root) + { + //base + if(root == null) return 0; + + //logic + int left = height(root.left); + int right = height(root.right); + + if(left == -1 || right == -1) return -1; + if(Math.abs(left-right) > 1) return -1; + + return Math.max(left, right) + 1; + } +} diff --git a/Palindrome Linked List Question b/Palindrome Linked List Question new file mode 100644 index 00000000..17821b91 --- /dev/null +++ b/Palindrome Linked List Question @@ -0,0 +1,22 @@ +234. Palindrome Linked List {Easy} + +Given the head of a singly linked list, return true if it is a palindrome or false otherwise. + +Example 1: + +Input: head = [1,2,2,1] +Output: true + +Example 2: + +Input: head = [1,2] +Output: false + + +Constraints: + +The number of nodes in the list is in the range [1, 105]. +0 <= Node.val <= 9 + + +Follow up: Could you do it in O(n) time and O(1) space? \ No newline at end of file diff --git a/PalindromeLinkedList.java b/PalindromeLinkedList.java new file mode 100644 index 00000000..e58f054d --- /dev/null +++ b/PalindromeLinkedList.java @@ -0,0 +1,85 @@ +/* +Time complexity : O(n), where n is the number of nodes in the Linked List. +Finding the middle is O(n), reversing a list in place is O(n), and then comparing the 2 resulting Linked Lists +is also O(n) => O(n) + O(n) + O(n) = O(3n) = O(n) + +Space complexity : O(1) as we are not using any auxiliary data structure + +Did this code successfully run on Leetcode: Yes +*/ + +/** + * 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 PalindromeLinkedList { + public boolean isPalindrome(ListNode head) + { + //base case + if(head == null) return true; + + //logic + ListNode slow = head; + ListNode fast = head; + + //Finding the mid of the list + while(fast.next != null && fast.next.next != null) + { + slow = slow.next; + fast = fast.next.next; + } + + //Now we will be having the mid point at slow + ListNode head2 = slow.next; //slow.next here is the midpoint + slow.next = null; + + /* + 1 -> 2 -> 2 -> 3 -> 2 -> 2 -> 1 -> null (3 is midpoint which is also slow) + + After finding the midpoint + left sublist = 1 -> 2 -> 2 -> 3 -> null (3 is head2) + right sublist = 2 -> 2 -> 1 -> null + */ + + /* + Now we need to reverse the right sublist to compare it with left sublist, make head2 point to the reversed list first node + meaning now it is pointing 2 make it point to 1 + */ + head2 = reverseList(head2); + + //After getting the reverse list, check whether elements in both the list are equal + while(head != null && head2 != null) + { + if(head.val != head2.val) { + return false; + } + + head = head.next; + head2 = head2.next; + } + + return true; + } + + private ListNode reverseList(ListNode head) + { + ListNode prev = null; + ListNode curr = head; + + while(curr != null) + { + ListNode nextTemp = curr.next; + curr.next = prev; + prev = curr; + curr = nextTemp; + } + + return prev; + } +} \ No newline at end of file diff --git a/Reverse Linked List Question b/Reverse Linked List Question new file mode 100644 index 00000000..c5e7e73a --- /dev/null +++ b/Reverse Linked List Question @@ -0,0 +1,26 @@ +https://leetcode.com/problems/reverse-linked-list/description/ + +206. Reverse Linked List {Easy} + +Given the head of a singly linked list, reverse the list, and return the reversed list. + +Example 1: + +Input: head = [1,2,3,4,5] +Output: [5,4,3,2,1] +Example 2: + +Input: head = [1,2] +Output: [2,1] +Example 3: + +Input: head = [] +Output: [] + +Constraints: + +The number of nodes in the list is the range [0, 5000]. +-5000 <= Node.val <= 5000 + + +Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both? \ No newline at end of file diff --git a/ReverseLinkedListIterative.java b/ReverseLinkedListIterative.java new file mode 100644 index 00000000..0d950831 --- /dev/null +++ b/ReverseLinkedListIterative.java @@ -0,0 +1,34 @@ +/* +Time complexity : O(n). List of n nodes, traverse over each node once + +Space complexity : O(1). + +Did this code successfully run on Leetcode: Yes +*/ + +//Definition for singly-linked list. +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 ReverseLinkedListIterative { + public ListNode reverseList(ListNode head) + { + ListNode prev = null; + ListNode curr = head; + + while(curr != null) + { + ListNode nextTemp = curr.next; + curr.next = prev; + prev = curr; + curr = nextTemp; + } + + return prev; + } +} diff --git a/ReverseLinkedListRecursive.java b/ReverseLinkedListRecursive.java new file mode 100644 index 00000000..ca7fd0c8 --- /dev/null +++ b/ReverseLinkedListRecursive.java @@ -0,0 +1,34 @@ +/* +Time complexity : O(n). List of n nodes, traverse over each node once + +Space complexity : O(n) +The extra space comes from implicit stack space due to recursion. The recursion could go up to n levels deep. Means, +so list of length n we make n recursive calls. + +Did this code successfully run on Leetcode: Yes +*/ + +/** + * 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 ReverseLinkedListRecursive { + public ListNode reverseList(ListNode head) + { + //base case + if( head == null || head.next == null) return head; + + //logic + ListNode reverseSubListHead = reverseList(head.next); + head.next.next = head; + head.next = null; + + return reverseSubListHead; + } +} \ No newline at end of file