LeetCode 783. Minimum Distance Between BST Nodes Posted on 2021-04-13 Edited on 2024-11-24 In LeetCode RecursionIteration1234567891011121314const minDiffInBST = (root) => { let ans = Number.MAX_SAFE_INTEGER, prev = -1; const dfs = (root) => { if (root) { dfs(root.left); if (prev != -1) ans = Math.min(ans, root.val - prev); prev = root.val; dfs(root.right); } }; dfs(root); return ans;};12345678910111213141516const minDiffInBST = (root) => { let ans = Number.MAX_SAFE_INTEGER, prev = -1; const stk = []; while (stk.length > 0 || root) { while (root) { stk.push(root); root = root.left; } root = stk.pop(); if (prev != -1) ans = Math.min(ans, root.val - prev); prev = root.val; root = root.right; } return ans;};