Answer the question
In order to leave comments, you need to log in
How to populate a binary tree correctly?
There is an initial problem, we solve it, we get a solution and write it to the root of the tree. After that, we divide the current task into two subtasks, adding to it one condition each (the conditions are different). We get two subtasks - two new nodes. Each node is divided in the same way into two more, each of which has one subtask obtained from the previous subtask. Etc.
How, for example, a search tree is built, I imagine - but there, each time, by condition, we go from the beginning of the tree in search of the node we need, to which we need to attach the next node - and here everything turns out right on the go should be built.
In general, in my thoughts everything is simple and clear, but I got confused in the implementation. Suggest an idea.
Answer the question
In order to leave comments, you need to log in
write the current value on the return from the recursion: we entered the tree node, launched the "left" subtask, launched the "right" subtask, calculated the result and wrote it to the current node. That is, going down, you solve problems by decomposition, and on the way back, write down the results, building a tree.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question