-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,45 @@ | ||
# Solution for: https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/ | ||
# Definition for singly-linked list. | ||
# | ||
# defmodule ListNode do | ||
# @type t :: %__MODULE__{ | ||
# val: integer, | ||
# next: ListNode.t() | nil | ||
# } | ||
# defstruct val: 0, next: nil | ||
# end | ||
|
||
# Definition for a binary tree node. | ||
# | ||
# defmodule TreeNode do | ||
# @type t :: %__MODULE__{ | ||
# val: integer, | ||
# left: TreeNode.t() | nil, | ||
# right: TreeNode.t() | nil | ||
# } | ||
# defstruct val: 0, left: nil, right: nil | ||
# end | ||
|
||
defmodule Solution do | ||
|
||
defp parse_list(nil), do: [] | ||
|
||
defp parse_list(head) do | ||
[head.val] ++ parse_list(head.next) | ||
end | ||
|
||
defp get_BST(list, left, right) do | ||
if left <= right do | ||
mid = div(left + right, 2) | ||
%TreeNode{val: Enum.at(list, mid), left: get_BST(list, left, mid - 1), right: get_BST(list, mid + 1, right)} | ||
else | ||
nil | ||
end | ||
end | ||
|
||
@spec sorted_list_to_bst(head :: ListNode.t | nil) :: TreeNode.t | nil | ||
def sorted_list_to_bst(head) do | ||
list = parse_list(head) | ||
get_BST(list, 0, Enum.count(list) - 1) | ||
end | ||
end |