0

I am working on a problem in leetcode. the code is written in javascript.

https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/

Whenever I test the code it returns undefined, however when i put console.log(true) right before my return statement, it prints true, but still does not return true.

var findTarget = function (root, k) {
  var stack = [];
  var currentNode = root;
  var arrayofVals = [];

  var traverse = function (currentNode) {
    console.log(currentNode);
    console.log(stack);
    arrayofVals.push(currentNode.val);

    if (currentNode.right !== null) {
      stack.push(currentNode.right);
    }

    if (currentNode.left !== null) {
      currentNode = currentNode.left;
      traverse(currentNode);
    }

    if (stack.length > 0 ) {
      currentNode = stack.pop();
      traverse(currentNode);
    } else {
      console.log(arrayofVals)

      for (var i = 0; i <= arrayofVals.length; i++) {
        for (var j = 0; j <= arrayofVals.length; j++) {
          if (i === j) {
            continue;
          }
          if(arrayofVals[i] + arrayofVals[j] === k) {
            console.log(1 === 1);
            return (1 === 1);
          }
        }
      }
      return false;
    }
  }
  traverse(currentNode);   
}

Can someone help me understand why my code returns undefined? I have had this problem before, but only when returning bool.

Thanks for your help!

1 Answer 1

3

Right now, findTarget isn't returning anything. You'll want to do return traverse(currentNode);.

Sign up to request clarification or add additional context in comments.

1 Comment

Makes Sense. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.