stevensonmt
I simply could not figure out a way to solve this problem without using a GenServer to keep the state of the running sum. My solution with GenServer is below, but I’m wondering if anyone could suggest a way to do it without a GenServer, which felt like overkill.
use GenServer
def init(tally), do: {:ok, tally}
def handle_cast({:add, n}, tally), do: {:noreply, n + tally}
def handle_call(:tally, _from, tally), do: {:reply, tally, tally}
@spec convert_bst(root :: TreeNode.t | nil) :: TreeNode.t | nil
def convert_bst(root) do
{:ok, pid} = GenServer.start(__MODULE__, 0)
do_convert(root, pid)
end
def do_convert(nil, tally), do: nil
def do_convert(node, tally) do
right = do_convert(node.right, tally)
GenServer.cast(tally, {:add, node.val})
new_val = GenServer.call(tally, :tally)
left = do_convert(node.left, tally)
%TreeNode{ val: new_val, left: left, right: right}
end
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Marked As Solved- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
The GenServer lets
do_convertdo two things:To remove it, you’ll need to transform that side-effect into a part of the return value.
Consider
Map.popfor inspiration; it returns a{popped_value, updated_map}tuple. You can combine it with rebinding to get code that looks almost like mutability:A similar transformation on
do_convertwould make it return a tuple of{tree_node, updated_tally}.Also Liked
stevensonmt
Many thanks. I knew there must be some way to keep that “state” without using a GenServer. My first issue had actually been figuring out how to use the left node value as the tally for the grandparent node calculation. I only figured that out after changing to GenServer which made the steps in the process clearer to me. Changing to returning tuples was trivial. Thanks again.
Aetherus
I came up with a similar solution, and it passed the tests:
carryis the sum of the values of all the nodes that have already been visited that have values greater than the next node to visit.joaoevangelista
I have no ideia how to implement such algorithm, but!
Genservers are an abstraction over a looping receiver, so you can try to implement it with recursion, keeping the state as a parameter and keep passing it. Start with the base cases that returns a value instead of recusing. I bet it will look closely to your current implementation,but without the genserver specifics
Last Post!
stackcats
There are many problems in Leetcode with the topic
Designthat you can’t do without using GenServer