How can I Flatten a tree (inorder traversal) for following Tree structure: https://gist.github.com/damadamdam/7b6364220b11871f2930

My expected answer is also attached with the gist.

有帮助吗?

解决方案

An Either contains a field of type A, or a field of type B, but never both. You can pass a Function to its ifLeft() method which will only be called if it contains an A. And you can pass a Function to its ifRight() method that will be called if it contains a B. SO, if you call both methods, one and only of the functions will be called.

Function is simply an interface that you can implement, and which transforms something into something else.

A Tree is either a single element, or a Triple of three Trees (being themselves either an element, or a Triple of three trees, etc.), forming a recursive data structure.

And a Triple has a left, a middle, and a right elements.

To traverse the tree, you should check if it contains a single element or a Triple. If it's a single element, the traversal is finished. If it's a Triple, then you should traverse the its left tree, its middle tree, and its right tree.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top