Skip to content

Commit

Permalink
*
Browse files Browse the repository at this point in the history
  • Loading branch information
imteekay committed Dec 9, 2023
1 parent 4c339cf commit 6e550a5
Showing 1 changed file with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function evaluateExpressionTree(tree) {
if (!tree) return null;

let leftValue = evaluateExpressionTree(tree.left);
let rightValue = evaluateExpressionTree(tree.right);

if (!leftValue && !rightValue) return tree.value;

if (tree.value === -1) return leftValue + rightValue;
if (tree.value === -2) return leftValue - rightValue;
if (tree.value === -3) return Math.trunc(leftValue / rightValue);
if (tree.value === -4) return leftValue * rightValue;
}

0 comments on commit 6e550a5

Please sign in to comment.