From 5f4e52a437390d538182a8a8c91944775a7f4604 Mon Sep 17 00:00:00 2001 From: ktony Date: Tue, 19 Nov 2024 21:26:20 -0500 Subject: [PATCH 1/5] Subtree Of Another Tree --- subtree-of-another-tree/TonyKim9401.java | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 subtree-of-another-tree/TonyKim9401.java diff --git a/subtree-of-another-tree/TonyKim9401.java b/subtree-of-another-tree/TonyKim9401.java new file mode 100644 index 000000000..0556bb3e2 --- /dev/null +++ b/subtree-of-another-tree/TonyKim9401.java @@ -0,0 +1,25 @@ +// TC: O(n) +// need to check all nodes in the worst case +// SC: O(n) +// recursion requires O(n) SC in the worst case +class Solution { + public boolean isSubtree(TreeNode root, TreeNode subRoot) { + if (root == null) return false; + + if (sameCheck(root, subRoot)) return true; + + return isSubtree(root.left, subRoot) || + isSubtree(root.right, subRoot); + } + + private boolean sameCheck(TreeNode root, TreeNode subRoot) { + if (root == null || subRoot == null) { + return root == null && subRoot == null; + } + + if (root.val != subRoot.val) return false; + + return sameCheck(root.left, subRoot.left) && + sameCheck(root.right, subRoot.right); + } +} From 06d77e2fb127a9c03fd405a5b50207917aa663e3 Mon Sep 17 00:00:00 2001 From: ktony Date: Wed, 20 Nov 2024 17:00:08 -0500 Subject: [PATCH 2/5] Validate Binary Search Tree --- validate-binary-search-tree/TonyKim9401.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 validate-binary-search-tree/TonyKim9401.java diff --git a/validate-binary-search-tree/TonyKim9401.java b/validate-binary-search-tree/TonyKim9401.java new file mode 100644 index 000000000..0eb18e48b --- /dev/null +++ b/validate-binary-search-tree/TonyKim9401.java @@ -0,0 +1,17 @@ +// TC: O(n) +// visit all nodes to compare +// SC: O(n) +// recursion requires O(n) SC in the worst case +class Solution { + public boolean isValidBST(TreeNode root) { + return dfs(root, Long.MIN_VALUE, Long.MAX_VALUE); + } + + private boolean dfs(TreeNode node, long min, long max) { + if (node == null) return true; + + if (node.val <= min || node.val >= max) return false; + return dfs(node.left, min, node.val) && + dfs(node.right, node.val, max); + } +} From 1f134cd34ff5c154a8f1b649df463f2216be7414 Mon Sep 17 00:00:00 2001 From: ktony Date: Wed, 20 Nov 2024 17:00:23 -0500 Subject: [PATCH 3/5] Longest Palindromic Substring --- .../TonyKim9401.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 longest-palindromic-substring/TonyKim9401.java diff --git a/longest-palindromic-substring/TonyKim9401.java b/longest-palindromic-substring/TonyKim9401.java new file mode 100644 index 000000000..3bb2f9183 --- /dev/null +++ b/longest-palindromic-substring/TonyKim9401.java @@ -0,0 +1,31 @@ +// TC: O(n^2) +// for loop & nested while loop => total O(n^2) +// SC: O(1) +// uses only constant space => O(1) +class Solution { + public String longestPalindrome(String s) { + int right = 0; + int left = 0; + + for (int i = 0; i < s.length(); i++) { + int even = pad(s, i, i); + int odd = pad(s, i, i+1); + int max = Math.max(even, odd); + + if (max > (right - left)) { + right = i + (max + 1) / 2; + left = i - max / 2; + } + } + + return s.substring(left, right+1); + } + + private int pad(String s, int start, int end) { + while (start >= 0 && end < s.length() && s.charAt(start) == s.charAt(end)) { + start -= 1; + end += 1; + } + return start - end; + } +} From 596556f72dce9bc4397ce2741a6e11ca0c2116c4 Mon Sep 17 00:00:00 2001 From: ktony Date: Thu, 21 Nov 2024 23:14:51 -0500 Subject: [PATCH 4/5] Rotate Image --- rotate-image/TonyKim9401.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 rotate-image/TonyKim9401.java diff --git a/rotate-image/TonyKim9401.java b/rotate-image/TonyKim9401.java new file mode 100644 index 000000000..cf7d43208 --- /dev/null +++ b/rotate-image/TonyKim9401.java @@ -0,0 +1,19 @@ +// TC: O(n^2) +// visit all elements with nested for-loop +// SC: O(1) +// constant space complexity +class Solution { + public void rotate(int[][] matrix) { + int n = matrix.length; + + for (int i = 0; i < (n + 1) / 2; i++) { + for (int j = 0; j < n / 2; j++) { + int temp = matrix[n-1-j][i]; + matrix[n-1-j][i] = matrix[n-1-i][n-1-j]; + matrix[n-1-i][n-1-j] = matrix[j][n-1-i]; + matrix[j][n-1-i] = matrix[i][j]; + matrix[i][j] = temp; + } + } + } +} From 9c14f52aef1d13a67943c65a09808d16ccb7b925 Mon Sep 17 00:00:00 2001 From: ktony Date: Fri, 22 Nov 2024 13:26:41 -0500 Subject: [PATCH 5/5] Subtree Of Another Tree TC, SC re-calculation --- subtree-of-another-tree/TonyKim9401.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/subtree-of-another-tree/TonyKim9401.java b/subtree-of-another-tree/TonyKim9401.java index 0556bb3e2..769be930d 100644 --- a/subtree-of-another-tree/TonyKim9401.java +++ b/subtree-of-another-tree/TonyKim9401.java @@ -1,7 +1,7 @@ -// TC: O(n) -// need to check all nodes in the worst case -// SC: O(n) -// recursion requires O(n) SC in the worst case +// TC: O(n * m) +// need to check all nodes and subRoot nodes in the worst case +// SC: O(h1 + h2) +// the high of root = h1 + the high of subRoot h2 => O(h1 + h2) class Solution { public boolean isSubtree(TreeNode root, TreeNode subRoot) { if (root == null) return false;