{"id":1796,"date":"2018-02-10T22:01:13","date_gmt":"2018-02-11T06:01:13","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1796"},"modified":"2018-02-11T14:06:59","modified_gmt":"2018-02-11T22:06:59","slug":"leetcode-783-minimum-distance-between-bst-nodes","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-783-minimum-distance-between-bst-nodes\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 783. Minimum Distance Between BST Nodes"},"content":{"rendered":"<div class=\"question-description\">\n<div>\n<p>Given a Binary Search Tree (BST) with the root node\u00a0<code>root<\/code>, return\u00a0the minimum difference between the values of any two different nodes in the tree.<\/p>\n<p><strong>Example :<\/strong><\/p>\n<pre class=\"\">Input: root = [4,2,6,1,3,null,null]\r\nOutput: 1\r\nExplanation:\r\nNote that root is a TreeNode object, not an array.\r\n\r\nThe given tree [4,2,6,1,3,null,null] is represented by the following diagram:\r\n\r\n          4\r\n        \/   \\\r\n      2      6\r\n     \/ \\    \r\n    1   3  \r\n\r\nwhile the minimum difference in this tree is 1, it occurs between node 1 and node 2, also between node 3 and node 2.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li>The size of the BST will be between 2 and\u00a0<code>100<\/code>.<\/li>\n<li>The BST is always valid, each node&#8217;s value is an integer, and each node&#8217;s value is different.<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<p><b>Solution 1: In order traversal\u00a0<\/b><\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 783\r\nclass Solution {\r\npublic:\r\n  int minDiffInBST(TreeNode* root) {\r\n    min_diff_ = INT_MAX;\r\n    prev_ = nullptr;\r\n    minDiff(root);\r\n    return min_diff_;\r\n  }\r\nprivate:\r\n  int min_diff_;\r\n  int* prev_;\r\n  void minDiff(TreeNode* root) {\r\n    if (root == nullptr) return;    \r\n    minDiff(root-&gt;left);\r\n    if (prev_ != nullptr)\r\n      min_diff_ = min(min_diff_, abs(root-&gt;val - *prev_));\r\n    prev_ = &amp;root-&gt;val;\r\n    minDiff(root-&gt;right);\r\n  }\r\n};<\/pre>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-530-minimum-absolute-difference-in-bst\/\">\u82b1\u82b1\u9171 LeetCode 530. Minimum Absolute Difference in BST<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Given a Binary Search Tree (BST) with the root node\u00a0root, return\u00a0the minimum difference between the values of any two different nodes in the tree. Example&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[163,45],"tags":[74],"class_list":["post-1796","post","type-post","status-publish","format-standard","hentry","category-easy","category-tree","tag-bst","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=1796"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1796\/revisions"}],"predecessor-version":[{"id":1800,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1796\/revisions\/1800"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}