<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Binary Search Tree on Yosgi</title><link>https://yosgi.github.io/zh/tags/binary-search-tree/</link><description>Recent content in Binary Search Tree on Yosgi</description><generator>Hugo</generator><language>zh</language><lastBuildDate>Mon, 29 Jun 2026 09:20:52 +0000</lastBuildDate><atom:link href="https://yosgi.github.io/zh/tags/binary-search-tree/index.xml" rel="self" type="application/rss+xml"/><item><title>783-二叉搜索树节点最小距离</title><link>https://yosgi.github.io/zh/post/783-%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E8%8A%82%E7%82%B9%E6%9C%80%E5%B0%8F%E8%B7%9D%E7%A6%BB/</link><pubDate>Thu, 18 Mar 2021 00:00:00 +1300</pubDate><guid>https://yosgi.github.io/zh/post/783-%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E8%8A%82%E7%82%B9%E6%9C%80%E5%B0%8F%E8%B7%9D%E7%A6%BB/</guid><description>用时 ： 10min 利用前序遍历二叉树递增的性质解决 var minDiffInBST = function(root) { var min = Infinity var pre = null var dfs = function (root) { if (!root) return if (root.left) { dfs(root.left) } if (pre == null) { pre = root.val } else { console.log(pre,root.val) var reduce = Math.abs(pre - root.val) min = Math.min(min,reduce) pre = root.val } if (root.right) { dfs(root.right) } } dfs(root) return min };</description></item><item><title>99-恢复二叉搜索树</title><link>https://yosgi.github.io/zh/post/99-%E6%81%A2%E5%A4%8D%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91/</link><pubDate>Sat, 13 Mar 2021 00:00:00 +1300</pubDate><guid>https://yosgi.github.io/zh/post/99-%E6%81%A2%E5%A4%8D%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91/</guid><description>用时：60min 思路还是中序遍历，使用pre缓存前一个节点，因为中序遍历是递增的，所以一定是先找到大的，再找到小的。 所以第一个出问题的是pre，第二个是root var recoverTree = function(root) { var left = null var right = null var pre = null // 一定是先找到大的，再找到小的，所以第一个出问题的是pre，第二个出问题的是root var DFS = function (root) { if (root.left) { DFS(root.left) } if (pre) { console.log(pre.val) if (pre.val &gt; root.val) { if(!left) { left = pre right = root } else { right = root } } pre = root } else { pre = root } if (root.right) { DFS(root.right) } } DFS(root) if (left &amp;&amp; right) { [left.val,right.val] = [right.val,left.val] } return root}</description></item><item><title>98-验证二叉搜索树</title><link>https://yosgi.github.io/zh/post/98-%E9%AA%8C%E8%AF%81%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91/</link><pubDate>Sat, 13 Mar 2021 00:00:00 +1300</pubDate><guid>https://yosgi.github.io/zh/post/98-%E9%AA%8C%E8%AF%81%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91/</guid><description>用时 10min 直接用二叉搜索树中序遍历递增的性质就好了 中序遍历找到左孩子 pre 是否存在 不存在，赋值 ，找右孩子，返回 1 存在，是否比 val 小 是，非递增，返回 false 否，递增，赋值 pre ，返回 1 var isValidBST = function(root) { var preVal = null var DFS = function(root) { var left = true var right = true if (root.left) { left = DFS(root.left) } if (preVal !== null) { if (preVal &gt;= root.val) return false preVal = root.val } else { preVal = root.val } if (root.right) { right = DFS(root.right) } return left &amp;&amp; right } return DFS(root) };</description></item><item><title>669-修剪二叉搜索树</title><link>https://yosgi.github.io/zh/post/669-%E4%BF%AE%E5%89%AA%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91/</link><pubDate>Thu, 11 Mar 2021 00:00:00 +1300</pubDate><guid>https://yosgi.github.io/zh/post/669-%E4%BF%AE%E5%89%AA%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91/</guid><description>用时 ：20min 与上一道题类似，多的是分了父节点在区间内和不在区间内的情况 var trimBST = function(root, low, high) { var add = function (root,node) { if (!node || !root) return null if (node.val &gt; root.val ) { if (!root.right) { root.right = node } else { add(root.right,node) } } if (node.val &lt; root.val) { if (!root.left) { root.left = node } else { add(root.left,node) } } } var walk = function (root) { if (!root) return root if (root.val &lt; low || root.val &gt; high) { var left = walk(root.left) var right = walk(root.right) if (!left &amp;&amp; !right) { root = null } else if (right &amp;&amp; left) { root = right add(root,left) } else if (left) { root = left } else { root = right } } else { root.left= walk(root.left) root.right = walk(root.right) } return root } return walk(root) };看了解析之后发现可以更简单，思路是如果大于右边界，就直接在左分支找； 如果小于左边界，就直接在右分支找。这样就省去了删除节点的步骤 var trimBST = function(root, low, high) { if(root == null) return null if (root.val &gt; high) return trimBST(root.left,low,high) if(root.val &lt; low) return trimBST(root.right,low,high) root.left = trimBST(root.left,low,height) root.right = trimBST(root.right,low,height) return root};</description></item><item><title>450-删除二叉搜索树中的节点</title><link>https://yosgi.github.io/zh/post/450-%E5%88%A0%E9%99%A4%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9/</link><pubDate>Thu, 11 Mar 2021 00:00:00 +1300</pubDate><guid>https://yosgi.github.io/zh/post/450-%E5%88%A0%E9%99%A4%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9/</guid><description>用时:30min 题目分两个部分需要解决，搜索和删除 搜索部分就是找到节点，可以根据二叉搜索树的性质来找到需要的节点 删除分三种情况 只有左孩子，用左孩子代替 只有右孩子，用右孩子代替 都没有，则赋值为null （ 由于前面有递归赋值，所以这边可以成功 都有，则储存左孩子后，用右孩子代替，然后找到左孩子在右边的位置 var deleteNode = function(root, key) { function insert(root,node) { if (root.val &gt; node.val) { if (!root.left) { root.left = node } else { insert(root.left,node) } } else { if (!root.right) { root.right = node } else { insert(root.right,node) } } } function search(root) { if (!root) { return null } if (root.val &gt; key) { root.left = search(root.left) } else if (root.val &lt; key){ root.right = search(root.right) } else { if (!root.left &amp;&amp; !root.right) { root = null } else if (root.left &amp;&amp; root.right) { var left = root.left root = root.right insert(root,left) } else if (root.left) { root = root.left } else if (root.right) { root = root.right } } return root } return search(root) };需要注意的点 考虑节点不存在的情况 删除一个节点的方法，可以给左（右 节点赋值为递归的返回值</description></item><item><title>1008-前序遍历构造二叉搜索树</title><link>https://yosgi.github.io/zh/post/1008-%E5%89%8D%E5%BA%8F%E9%81%8D%E5%8E%86%E6%9E%84%E9%80%A0%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91/</link><pubDate>Wed, 10 Mar 2021 00:00:00 +1300</pubDate><guid>https://yosgi.github.io/zh/post/1008-%E5%89%8D%E5%BA%8F%E9%81%8D%E5%8E%86%E6%9E%84%E9%80%A0%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91/</guid><description>用时 ： 没做出来 没做出来的主要原因是一直想着怎么用栈来做，写到后面看栈的答案看着看着发现递归最简单 递归 思路很简单，首先写出来怎么插入子节点 如果比父节点小，且父节点左为空，直接做左节点 如果比父节点大，且父节点右为空，直接做右节点 比父节点小，则父节点赋值为父节点的左节点，回到 1 比父节点大，则父节点赋值为父节点的右节点，回到1 遍历整个数组，一个个插入即可得到结果 var bstFromPreorder = function(preorder) { var add = function (node,val) { if (val &lt; node.val &amp;&amp; !node.left) { node.left = new TreeNode(val) } if (val &gt; node.val &amp;&amp; !node.right) { node.right = new TreeNode(val) } if (val &lt; node.val) { add(node.left,val) } if (val &gt; node.val) { add(node.right,val) } } var root = new TreeNode(preorder.shift()) for(let i = 0 ; i &lt; preorder.length ; i ++) { add(root,preorder[i]) } return root};</description></item></channel></rss>