title: how to get difference between two values or nodes in javascript
body: <p>I am trying to solve this problem .I am not getting expected output</p>
  
  <p>Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree.</p>
  
  <p>Example :</p>
  
  <p><strong>Input:</strong> </p>
  
  <pre><code>root = [4,2,6,1,3,null,null]
  </code></pre>
  
  <p><strong>Output:</strong> </p>
  
  <pre><code>1
  </code></pre>
  
  <p><strong>Explanation:</strong></p>
  
  <blockquote>
    <p>Note that root is a TreeNode object, not an array.</p>
  </blockquote>
  
  <p>The given tree [4,2,6,1,3,null,null] is represented by the following diagram:</p>
  
  <pre><code>      4
      /   \
    2      6
   / \    
  1   3  
  </code></pre>
  
  <p>while the minimum difference in this tree is 1, it occurs between node 1 and node 2, also between node 3 and node 2.</p>
  
  <p>I tried like this</p>
  
  <pre><code>var minDiffInBST = function (root) {
      let min = Number.MAX_VALUE
      const getMin = (node) =&gt; {
          if (node.left &amp;&amp; node.right) {
              console.log('both')
              return Math.min(node.val - node.left.val, node.right.val - node.val)
          } else if (node.right) {
              console.log('right')
              return node.right.val - node.val
          } else if (node.left) {
              console.log('left')
              return node.val - node.left.val
          } else {
              return Number.MAX_VALUE
          }
      }
  
      const preOrder = (root) =&gt; {
          if (!root) {
              return 0;
          }
          let x = getMin(root)
          if (x &lt; min)
              min = x
          preOrder(root.left)
          preOrder(root.right)
  
      }
      preOrder(root)
      return min
  };
  
  console.log(minDiffInBST({
          "val": 90,
          "left": {
              "val": 69,
              "left": {"val": 49, "left": null, "right": {"val": 52, "left": null, "right": null}},
              "right": {"val": 89, "left": null, "right": null}
          },
          "right": null
      }
  
  ))
  </code></pre>
  
  <p>Getting output <code>3</code> 
  <strong>expected output</strong> <code>1</code></p>
  
  <p>question I am taken from here
  <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" rel="nofollow noreferrer">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>