# Evaluate Expression Tree

![](https://3743232000-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHW2IQuh2PFpWJDvBz2FF%2Fuploads%2Fgit-blob-0e370cefb969bd3ceaf37594be09986d8f69035c%2FScreenshot%202023-04-15%20at%2016.53.41.png?alt=media)

* n, h

```jsx
// This is an input class. Do not edit.
class BinaryTree {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const add = (a, b) => a + b;
const subtract = (a, b) => a - b;
const divide = (a, b) => a / b;
const multiply = (a, b) => a * b;

const operations = {
  '-1': add,
  '-2': subtract,
  '-3': divide,
  '-4': multiply
}

function evaluateExpressionTree(tree) {

  return evaluate(tree);
}

function evaluate(node) {
  if (node.value > 0) 
    return node.value;

  const operator = node.value;
  const operate = operations[operator];
  
  const left = evaluate(node.left);
  const right = evaluate(node.right);

  return parseInt(operate(left, right));
}

// Do not edit the lines below.
exports.BinaryTree = BinaryTree;
exports.evaluateExpressionTree = evaluateExpressionTree;
```
