-
Notifications
You must be signed in to change notification settings - Fork 0
108. Convert Sorted Array to Binary Search Tree #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
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; | ||
} | ||
} | ||
``` |
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; | ||
} | ||
} | ||
``` |
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]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. これ、なくても動きますか? (ちゃんと考えていない。) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 動きます。こちらの指摘と同じ疑問になったっていうところですか? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
開閉区間でも書いてみていただけますか?
There was a problem hiding this comment.
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]
There was a problem hiding this comment.
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() を実装したバージョンを書いてみていただけますでしょうか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この認識でいいんでしょうか?
77c7696