/    Sign up×
Community /Pin to ProfileBookmark

Can someone help me with this js problem?

### Tree Constructor

Have the function TreeConstructor( **strArr** ) take the array of strings stored in  **strArr** , which will contain pairs of integers in the following format:  **(i1,i2)** , where  **i1**  represents a child node in a tree and the second integer  **i2**  signifies that it is the parent of  **i1** . For example: if  **strArr**  is [“(1,2)”, “(2,4)”, “(7,2)”], then this forms the following tree:

![](https://i.imgur.com/NMRdSO1.png)

which you can see forms a proper binary tree. Your program should, in this case, return the string  **true**  because a valid binary tree can be formed. If a proper binary tree cannot be formed with the integer pairs, then return the string  **false** . All of the integers within the tree will be unique, which means there can only be one node in the tree with the given integer value.

#### Examples

Input: [“(1,2)”, “(2,4)”, “(5,7)”, “(7,2)”, “(9,5)”]
Output: true

Input: [“(1,2)”, “(3,2)”, “(2,12)”, “(5,2)”]
Output: false

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinAug 04.2020 — ``<i>
</i>&lt;script&gt;
Input1 = ["(1,2)", "(2,4)", "(5,7)", "(7,2)", "(9,5)"];
Input2 = ["(1,2)", "(3,2)", "(2,12)", "(5,2)"];
alert("Input1 is "+validate(Input1)+"nInput2 is "+validate(Input2));
function validate(arg){
var store=[];
var re;
for(var i=0;i&lt;arg.length;i++){
re=(eval('list'+arg[i]));
if(typeof store[re[1]]=='undefined')store[re[1]]=0;
if(++store[re[1]]&gt;2)return "invalade";
}
function list(a,b){return [a,b];}
return "valade";
}
&lt;/script&gt;<i>
</i>
``
×

Success!

Help @tommy4260 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 3.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...