{"id":5060,"date":"2019-04-13T00:36:05","date_gmt":"2019-04-13T07:36:05","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5060"},"modified":"2019-04-13T00:36:47","modified_gmt":"2019-04-13T07:36:47","slug":"leetcode-230-kth-smallest-element-in-a-bst","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-230-kth-smallest-element-in-a-bst\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 230. Kth Smallest Element in a BST"},"content":{"rendered":"\n<p>Given a binary search tree, write a function&nbsp;<code>kthSmallest<\/code>&nbsp;to find the&nbsp;<strong>k<\/strong>th smallest element in it.<\/p>\n\n\n\n<p><strong>Note:&nbsp;<\/strong><br>You may assume k is always valid, 1 \u2264 k \u2264 BST&#8217;s total elements.<\/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> root = [3,1,4,null,2], k = 1\n   3\n  \/ \\\n 1   4\n  \\\n&nbsp;  2\n<strong>Output:<\/strong> 1<\/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> root = [5,3,6,2,4,null,null,1], k = 3\n       5\n      \/ \\\n     3   6\n    \/ \\\n   2   4\n  \/\n 1\n<strong>Output:<\/strong> 3\n<\/pre>\n\n\n\n<p><strong>Follow up:<\/strong><br>What if the BST is modified (insert\/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?<\/p>\n\n\n\n<p>Solution: Inorder traversal<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space compleixty: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">class Solution {\npublic:\n  int kthSmallest(TreeNode* root, int k) {\n    return inorder(root, k);\n  }\nprivate:\n  int inorder(TreeNode* root, int& k) {\n    if (!root) return -1;\n    int x = inorder(root->left, k);\n    if (k == 0) return x;\n    if (--k == 0) return root->val;\n    return inorder(root->right, k);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary search tree, write a function&nbsp;kthSmallest&nbsp;to find the&nbsp;kth smallest element in it. Note:&nbsp;You may assume k is always valid, 1 \u2264 k \u2264&#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":[74,383],"class_list":["post-5060","post","type-post","status-publish","format-standard","hentry","category-tree","tag-bst","tag-kth","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5060","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=5060"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5060\/revisions"}],"predecessor-version":[{"id":5063,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5060\/revisions\/5063"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}