-
Notifications
You must be signed in to change notification settings - Fork 0
108. Convert Sorted Array to Binary Search Tree #24
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
Open
hiroki-horiguchi-dev
wants to merge
1
commit into
main
Choose a base branch
from
tree-bst-108
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| - 問題: [108. Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/) | ||
| - 条件 | ||
| - `1 <= nums.length <= 10^4` | ||
| - `-10^4 <= nums[i] <= 10^4` | ||
| - `nums is sorted in a strictly increasing order.` | ||
| - 方針 | ||
| - 再帰関数というか、2分探索 | ||
| - int middle = nums.length / 2 を真ん中とする | ||
| - 0 ~ middle までを左に振り | ||
| - middle + 1 ~ nums.length までを右に振る | ||
| - 上記をひたすら繰り返す | ||
| - 時間計算量: `O(log n)` | ||
| - 空間計算量: TreeNode を nums.length - 1 分作るので、`O(N)`, 再帰の深さは`O(log n)` | ||
| - 時間: 10分 | ||
|
|
||
| ```java | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * public 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 Solution { | ||
| public TreeNode sortedArrayToBST(int[] nums) { | ||
| if (nums.length == 0) { | ||
| return null; | ||
| } | ||
| return createTreeNode(nums, 0, nums.length); | ||
| } | ||
|
|
||
| private TreeNode createTreeNode(int[] nums, int left, int right) { | ||
| if (right <= 0 || left >= right) { | ||
| return null; | ||
| } | ||
|
|
||
| int middle = (left + right) / 2; | ||
| TreeNode root = new TreeNode(nums[middle]); | ||
| root.left = createTreeNode(nums, left, middle); | ||
| root.right = createTreeNode(nums, middle + 1, right); | ||
|
|
||
| return root; | ||
| } | ||
| } | ||
| ``` | ||
| - 条件が冗長だったので書き直し | ||
| ```java | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * public 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 Solution { | ||
| public TreeNode sortedArrayToBST(int[] nums) { | ||
| return createBST(nums, 0, nums.length); | ||
| } | ||
|
|
||
| private TreeNode createBST(int[] nums, int left, int right) { | ||
| if (left >= right) { | ||
| return null; | ||
| } | ||
| int middle = (left + right) / 2; | ||
| TreeNode newNode = new TreeNode(nums[middle]); | ||
| newNode.left = createBST(nums, left, middle); | ||
| newNode.right = createBST(nums, middle + 1, right); | ||
| return newNode; | ||
| } | ||
| } | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
int middle = left + (right - left) / 2;とするとオーバーフロー対策にはなりそうです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 について考える時はおっしゃる通り、その通りです。
今回の問題は 10^4 が最大値なので、コールスタックに積むのは14程度になるので問題としては考慮不要として解いていました。
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.
失礼しました。
ここでいただいている指摘は整数オーバーフローの方ですね。制約の通り、この問題ではオーバーフローしませんが、日常的に書いておくべきことだと思いました。
ありがとうございます。