<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/en/tags/binary-search-tree/</link><description>Recent content in Binary Search Tree on Yosgi</description><generator>Hugo</generator><language>en</language><lastBuildDate>Mon, 29 Jun 2026 09:20:51 +0000</lastBuildDate><atom:link href="https://yosgi.github.io/en/tags/binary-search-tree/index.xml" rel="self" type="application/rss+xml"/><item><title>783-Binary Search Tree Node Minimum Distance</title><link>https://yosgi.github.io/en/post/783-binary-search-tree-node-minimum-distance/</link><pubDate>Thu, 18 Mar 2021 00:00:00 +1300</pubDate><guid>https://yosgi.github.io/en/post/783-binary-search-tree-node-minimum-distance/</guid><description>Time: 10 minutes Solve by using the property of pre-order traversal of binary tree 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-Restore Binary Search Tree</title><link>https://yosgi.github.io/en/post/99-restore-binary-search-tree/</link><pubDate>Sat, 13 Mar 2021 00:00:00 +1300</pubDate><guid>https://yosgi.github.io/en/post/99-restore-binary-search-tree/</guid><description>Time: 60 minutes The idea is still in-order traversal, using pre to cache the previous node. Because the in-order traversal is incremental, you must find the larger one first and then the smaller one. So the first problem is pre, the second is root var recoverTree = function(root) { var left = null var right = null var pre = null // You must find the big one first, then the small one, so the first one to have a problem is pre, and the second one to have a problem is 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-Verify Binary Search Tree</title><link>https://yosgi.github.io/en/post/98-verify-binary-search-tree/</link><pubDate>Sat, 13 Mar 2021 00:00:00 +1300</pubDate><guid>https://yosgi.github.io/en/post/98-verify-binary-search-tree/</guid><description>Time: 10 minutes Just use the increasing property of in-order traversal of the binary search tree In-order traversal to find the left child Does pre exist? If not, assign , find the right child, and return 1 If it exists, is it less than val? If yes, do not increment, and return false If not, increment, assign pre, and return 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-Pruning a Binary Search Tree</title><link>https://yosgi.github.io/en/post/669-pruning-a-binary-search-tree/</link><pubDate>Thu, 11 Mar 2021 00:00:00 +1300</pubDate><guid>https://yosgi.github.io/en/post/669-pruning-a-binary-search-tree/</guid><description>#669. Pruning a binary search tree Release Date: March 11, 2021 Time: 20 minutes Similar to the previous question, most of them are divided into situations where the parent node is in the interval and not in the interval 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) };After reading the analysis, I found that it can be simpler. The idea is that if it is greater than the right boundary, look directly in the left branch; if it is less than the left boundary, look directly in the right branch. This saves the step of deleting the node.</description></item><item><title>450-Delete a node in a binary search tree</title><link>https://yosgi.github.io/en/post/450-delete-a-node-in-a-binary-search-tree/</link><pubDate>Thu, 11 Mar 2021 00:00:00 +1300</pubDate><guid>https://yosgi.github.io/en/post/450-delete-a-node-in-a-binary-search-tree/</guid><description>Time: 30 minutes The question is divided into two parts to be solved, search and delete The search part is to find the node. You can find the required node according to the properties of the binary search tree. There are three types of deletion If there is only a left child, replace it with the left child. If there is only a right child, replace it with the right child. If there is neither child, assign null. (Since there was a recursive assignment earlier, this will succeed.) If there are both children, store the left child, replace it with the right child, and then find the left child’s position on the right. 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) };Points to note</description></item><item><title>1008-Pre-order traversal to construct a binary search tree</title><link>https://yosgi.github.io/en/post/1008-pre-order-traversal-to-construct-a-binary-search-tree/</link><pubDate>Wed, 10 Mar 2021 00:00:00 +1300</pubDate><guid>https://yosgi.github.io/en/post/1008-pre-order-traversal-to-construct-a-binary-search-tree/</guid><description>Time: Not done The main reason I didn’t make it was that I kept thinking about how to use a stack. When I looked at the answer to the stack later, I found that recursion was the simplest. recursion The idea is very simple. First, write out how to insert a child node. If the node is smaller than the parent node and the parent node’s left node is empty, make it the left node. If the node is larger than the parent node and the parent node’s right node is empty, make it the right node. If the node is smaller than the parent node, assign the parent node to the parent node’s left node and return to 1. If the node is larger than the parent node, assign the parent node to the parent node’s right node and return to 1. Traverse the entire array and insert each node one by one to get the result. 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};Time: Not done</description></item></channel></rss>