From c772cb1bf5b9c5525ace8853c5b917490c7f8f7d Mon Sep 17 00:00:00 2001 From: rossy0213 Date: Mon, 15 Apr 2024 02:53:56 +0900 Subject: [PATCH] add: Convert Sorted Array to Binary Search Tree --- ConvertSortedArrayToBinarySearchTree/step1.md | 44 +++++++++++ ConvertSortedArrayToBinarySearchTree/step2.md | 24 ++++++ ConvertSortedArrayToBinarySearchTree/step3.md | 76 +++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 ConvertSortedArrayToBinarySearchTree/step1.md create mode 100644 ConvertSortedArrayToBinarySearchTree/step2.md create mode 100644 ConvertSortedArrayToBinarySearchTree/step3.md diff --git a/ConvertSortedArrayToBinarySearchTree/step1.md b/ConvertSortedArrayToBinarySearchTree/step1.md new file mode 100644 index 0000000..8638b30 --- /dev/null +++ b/ConvertSortedArrayToBinarySearchTree/step1.md @@ -0,0 +1,44 @@ +```java +// Thought it will be a binary search. +// Just use the way like binary search, put the mid node as parent +// Then put left and right side will be the children node of the parent. +// Repeat the process and we will have the tree. +// Time complexity: O(N) N: length of the given array +// Space complexity: O(N) +// Time spend: N/A (Checked Solution tab) +class Solution { + public TreeNode sortedArrayToBST(int[] nums) { + return sortedArrayToBST(nums, 0, nums.length - 1); + } + + public TreeNode sortedArrayToBST(int[] nums, int start, int end) { + if (start > end) { + return null; + } + + int mid = (start + end) / 2; + TreeNode root = new TreeNode(nums[mid]); + root.left = sortedArrayToBST(nums, start, mid - 1); + root.right = sortedArrayToBST(nums, mid + 1, end); + + return root; + } + + public TreeNode sortedArrayToBST(int[] nums) { + return sortedArrayToBST(nums, 0, nums.length); + } + + public TreeNode sortedArrayToBST(int[] nums, int start, int end) { + if (start >= end) { + return null; + } + + int mid = (start + end) / 2; + TreeNode root = new TreeNode(nums[mid]); + root.left = sortedArrayToBST(nums, start, mid); + root.right = sortedArrayToBST(nums, mid + 1, end); + + return root; + } +} +``` \ No newline at end of file diff --git a/ConvertSortedArrayToBinarySearchTree/step2.md b/ConvertSortedArrayToBinarySearchTree/step2.md new file mode 100644 index 0000000..35d575f --- /dev/null +++ b/ConvertSortedArrayToBinarySearchTree/step2.md @@ -0,0 +1,24 @@ +```java +class Solution { + public TreeNode sortedArrayToBST(int[] nums) { + return convertSortedArrayToBST(nums, 0, nums.length - 1); + } + + public TreeNode convertSortedArrayToBST(int[] nums, int start, int end) { + if (start > end) { + return null; + } + + if (start == end) { + return new TreeNode(nums[start]); + } + + int mid = (start + end) / 2; + TreeNode root = new TreeNode(nums[mid]); + root.left = convertSortedArrayToBST(nums, start, mid - 1); + root.right = convertSortedArrayToBST(nums, mid + 1, end); + + return root; + } +} +``` \ No newline at end of file diff --git a/ConvertSortedArrayToBinarySearchTree/step3.md b/ConvertSortedArrayToBinarySearchTree/step3.md new file mode 100644 index 0000000..1f2e8c0 --- /dev/null +++ b/ConvertSortedArrayToBinarySearchTree/step3.md @@ -0,0 +1,76 @@ +```java +// Time spend: 02:31 +class Solution { + public TreeNode sortedArrayToBST(int[] nums) { + return convertSortedArrayToBst(nums, 0, nums.length - 1); + } + + public TreeNode convertSortedArrayToBst(int[] nums, int l, int r) { + if (l > r) { + return null; + } + + if (l == r) { + return new TreeNode(nums[l]); + } + + int mid = (l + r) / 2; + TreeNode root = new TreeNode(nums[mid]); + root.left = convertSortedArrayToBst(nums, l, mid - 1); + root.right = convertSortedArrayToBst(nums, mid + 1, r); + + return root; + } +} +``` + +```java +// Time spend: 01:42 +class Solution { + public TreeNode sortedArrayToBST(int[] nums) { + return convertSortedArrayToBst(nums, 0, nums.length - 1); + } + + public TreeNode convertSortedArrayToBst(int[] nums, int l, int r) { + if (l > r) { + return null; + } + if (l == r) { + return new TreeNode(nums[l]); + } + + int mid = (l + r) / 2; + TreeNode root = new TreeNode(nums[mid]); + root.left = convertSortedArrayToBst(nums, l, mid - 1); + root.right = convertSortedArrayToBst(nums, mid + 1, r); + + return root; + } +} +``` + +```java +// Time spend: 01:42 +class Solution { + public TreeNode sortedArrayToBST(int[] nums) { + return convertSortedArrayToBst(nums, 0, nums.length - 1); + } + + public TreeNode convertSortedArrayToBst(int[] nums, int l, int r) { + if (l > r) { + return null; + } + + if (l == r) { + return new TreeNode(nums[l]); + } + + int mid = (l + r) / 2; + TreeNode root = new TreeNode(nums[mid]); + root.left = convertSortedArrayToBst(nums, l, mid - 1); + root.right = convertSortedArrayToBst(nums, mid + 1, r); + + return root; + } +} +``` \ No newline at end of file