Skip to content

Commit

Permalink
converting list into a BST
Browse files Browse the repository at this point in the history
  • Loading branch information
xsami committed Jan 5, 2024
1 parent 130157c commit 9d7259b
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions ListIntoBST/listIntoBST.exs
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

0 comments on commit 9d7259b

Please sign in to comment.