Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions ConvertSortedArrayToBinarySearchTree/step1.md
Original file line number Diff line number Diff line change
@@ -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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

開閉区間でも書いてみていただけますか?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

すみません、質問が理解できてなくて、こいうものを求めてるんでしょうか?
親のノードがnums[mid]だとしたときに、
左に入るものは [min_value, nums[mid]]
右に入るものは [nums[mid], max_value]

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最初の解法は [start, end] の閉区間で解いています。これとは別に、 [start, end) の半開区間で解きなおしていただけますでしょうか?

別の言い方をすると、
return sortedArrayToBST(nums, 0, nums.length);
と呼び出すよう、 sortedArrayToBST() を実装したバージョンを書いてみていただけますでしょうか?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この認識でいいんでしょうか?
77c7696

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;
}
}
```
24 changes: 24 additions & 0 deletions ConvertSortedArrayToBinarySearchTree/step2.md
Original file line number Diff line number Diff line change
@@ -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;
}
}
```
76 changes: 76 additions & 0 deletions ConvertSortedArrayToBinarySearchTree/step3.md
Original file line number Diff line number Diff line change
@@ -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]);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これ、なくても動きますか? (ちゃんと考えていない。)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

動きます。こちらの指摘と同じ疑問になったっていうところですか?
#13 (comment)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あ、そうですね。単に長くなっているので特別扱いする理由がない感覚です。

}

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;
}
}
```