algorithm - How do i write a function in JavaScript that compares two trees defined by TreeNodes a and b? - Stack Overflow

admin2025-04-19  1

I'm trying to write a JavaScript function that pares two binary trees defined by TreeNodes 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 TreeNodes 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)
Share Improve this question asked May 19, 2019 at 7:41 lasabahebwalasabahebwa 3013 silver badges15 bronze badges 4
  • 1 where is 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
  • So those remote tests expect a 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
  • that's right, i was expected to define pare though it wasn't indicated anywhere in the instructions. When i defined pare, all the tests passed. – lasabahebwa Commented May 19, 2019 at 14:56
Add a ment  | 

2 Answers 2

Reset to default 8

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745041612a281576.html

最新回复(0)