{"id":5620,"date":"2019-09-29T20:56:06","date_gmt":"2019-09-30T03:56:06","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5620"},"modified":"2019-09-29T21:00:57","modified_gmt":"2019-09-30T04:00:57","slug":"leetcode-99-recover-binary-search-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-99-recover-binary-search-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 99. Recover Binary Search Tree"},"content":{"rendered":"\n<p>Two elements of a binary search tree (BST) are swapped by mistake.<\/p>\n\n\n\n<p>Recover the tree without changing its structure.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> [1,3,null,null,2]\n\n&nbsp;  1\n&nbsp; \/\n&nbsp;3\n&nbsp; \\\n&nbsp;  2\n\n<strong>Output:<\/strong> [3,1,null,null,2]\n\n&nbsp;  3\n&nbsp; \/\n&nbsp;1\n&nbsp; \\\n&nbsp;  2\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> [3,1,4,null,null,2]\n\n  3\n \/ \\\n1   4\n&nbsp;  \/\n&nbsp; 2\n\n<strong>Output:<\/strong> [2,1,4,null,null,3]\n\n  2\n \/ \\\n1   4\n&nbsp;  \/\n &nbsp;3\n<\/pre>\n\n\n\n<p><strong>Follow up:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A solution using O(<em>n<\/em>) space is pretty straight forward.<\/li><li>Could you devise a constant space solution?<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Inorder traversal<\/strong><\/h2>\n\n\n\n<p>Using inorder traversal to find two nodes that have val &lt; prev.val<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(h)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  void recoverTree(TreeNode *root) {    \n    inorder(root);\n    swap(first->val, second->val);\n  }\n    \n  void inorder(TreeNode* root) {\n    if (!root) return;\n    inorder(root->left);\n    if (prev && prev->val > root->val) {\n      if (!first) first = prev;\n      second = root;\n    }\n    prev = root;\n    inorder(root->right);\n  }\nprivate:\n  TreeNode* first;\n  TreeNode* second;\n  TreeNode* prev;\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Example 1: Input: [1,3,null,null,2] &nbsp; 1&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[217,28],"class_list":["post-5620","post","type-post","status-publish","format-standard","hentry","category-tree","tag-hard","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5620","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=5620"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5620\/revisions"}],"predecessor-version":[{"id":5624,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5620\/revisions\/5624"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}