{"id":1896,"date":"2016-03-22T11:48:33","date_gmt":"2016-03-22T03:48:33","guid":{"rendered":"http:\/\/boweihe.me\/?p=1896"},"modified":"2016-03-22T11:48:33","modified_gmt":"2016-03-22T03:48:33","slug":"leetcode-230-kth-smallest-element-in-a-bst","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1896","title":{"rendered":"LeetCode 230. Kth Smallest Element in a BST"},"content":{"rendered":"<p>Given a binary search tree, write a function <code>kthSmallest<\/code> to find the <b>k<\/b>th smallest element in it.<br \/>\n<b>Note: <\/b><br \/>\nYou may assume k is always valid, 1 \u2264 k \u2264 BST&#8217;s total elements.<br \/>\n<b>Follow up:<\/b><br \/>\nWhat 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<h3>\u601d\u8def\uff1a<\/h3>\n<p>\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u4e2d\u5e8f\u904d\u5386\uff08LNR\uff09\u5c31\u662f\u6309\u7167\u4ece\u5c0f\u5230\u5927\u987a\u5e8f\u6392\u7684\uff0c\u6240\u4ee5\u904d\u5386\u5230N\u8282\u70b9\u7684\u65f6\u5019\u5c31\u8bb0\u4e2a\u6570\uff0c\u8ba1\u6570\u5230k\u7684\u65f6\u5019\u5c31\u8fd4\u56de\u4e86\u3002<\/p>\n<h3>\u4ee3\u7801\uff1a<\/h3>\n<pre class=\"lang:c++ decode:true \">\/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}\n * };\n *\/\nclass Solution {\npublic:\n    int kthSmallest(TreeNode* root, int k) {\n        if(root == NULL)\n            return -1;\n        int retVal = -1;\n        int count = 0;\n        kthSmallest1(root, k, count, retVal);\n        return retVal;\n    }\n    void kthSmallest1(TreeNode* root, int k, int&amp; count, int&amp; retVal){\n        if(count &gt;= k)\n            return;\n        if(root-&gt;left != NULL)\n            kthSmallest1(root-&gt;left, k, count, retVal);\n        count ++;\n        if(count == k){\n            retVal = root-&gt;val;\n            return;\n        }\n        if(root-&gt;right != NULL)\n            kthSmallest1(root-&gt;right, k, count, retVal);\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 \u2264 k \u2264 BST&#8217;s total elements. Follow up: What if the BST is modified (insert\/delete operations) often and you need to find the kth smallest frequently? How would you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66],"class_list":["post-1896","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1896","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1896"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1896\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}