I'm trying to write a JavaScript function that pares two binary trees defined by TreeNode
s a and b and returns true if they are equal in structure and in value and false otherwise.
for example example of paring both values and structure of two binary trees
Given the following class:
class TreeNode {
constructor(data, left=null, right=null) {
this.data = data;
this.left = left;
this.right = right;
}
}
Here is the code i tried writing so far apring TreeNode a and b.
const binaryTreeCompare = (a, b) => {
if(a==null && b==null){
return true;
}else if(a!=null && b!=null){
return(
a.data == b.data && binaryTreeCompare(a.left, b.left) && binaryTreeCompare(a.right, b.right)
);
}
else return false;
}
I expected an ouput of either true or false but this is what i get:
ReferenceError: pare is not defined
at Context.it (test.js:116:16)
I'm trying to write a JavaScript function that pares two binary trees defined by TreeNode
s a and b and returns true if they are equal in structure and in value and false otherwise.
for example example of paring both values and structure of two binary trees
Given the following class:
class TreeNode {
constructor(data, left=null, right=null) {
this.data = data;
this.left = left;
this.right = right;
}
}
Here is the code i tried writing so far apring TreeNode a and b.
const binaryTreeCompare = (a, b) => {
if(a==null && b==null){
return true;
}else if(a!=null && b!=null){
return(
a.data == b.data && binaryTreeCompare(a.left, b.left) && binaryTreeCompare(a.right, b.right)
);
}
else return false;
}
I expected an ouput of either true or false but this is what i get:
ReferenceError: pare is not defined
at Context.it (test.js:116:16)
pare
from?
– Nina Scholz
Commented
May 19, 2019 at 8:14
pare
is not anywhere in my code, I'm running my code against some tests remotely that's why I'm getting a ReferenceError with pare
– lasabahebwa
Commented
May 19, 2019 at 8:49
pare
function to be defined? If you don't use pare
in your code, the error is not your fault. (Or you were expected to define pare
, but did define it with the name binaryTreeCompare
instead).
– Bergi
Commented
May 19, 2019 at 14:52
Solution to my own question after serious research is shown in the snippet below.
function pare(a, b){
if (!a && !b) {
return true;
} else if (!a || !b) {
return false;
} else {
return a.val === b.val && pare(a.left, b.left) && pare(a.right, b.right);
}
}
One quick-and-dirty approach could be to define a canonical serialization for trees, and then pare them.
The simplest approach would be to JSON.stringify each tree. You'd need to implement a custom toJSON
method for TreeNode
.
class TreeNode {
constructor(data, left=null, right=null) {
this.data = data;
this.left = left;
this.right = right;
}
toJSON() {
return JSON.stringify({ data: this.data, left: this.left, right: this.right });
}
}
Then, binaryTreeCompare
bees trivial.
EDIT: once you've defined a custom toJSON
method on TreeNode
, then binaryTreeCompare
bees this:
function binaryTreeCompare(a, b) {
return JSON.stringify(a) === JSON.stringify(b)
}
However, the error message you report has nothing to do with your algorithm. It's hard to know for sure what the problem is, because the error message references something that doesn't appear in your sample code. I suspect your real code differs from the code you posted in a way that is critical to the problem.