{"id":3092,"date":"2018-07-12T08:24:50","date_gmt":"2018-07-12T15:24:50","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3092"},"modified":"2018-07-12T08:24:59","modified_gmt":"2018-07-12T15:24:59","slug":"leetcode-700-search-in-a-binary-search-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-700-search-in-a-binary-search-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 700. Search in a Binary Search Tree"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node&#8217;s value equals the given value. Return the subtree rooted with that node. If such node doesn&#8217;t exist, you should return NULL.<\/p>\n<p>For example,<\/p>\n<pre class=\"crayon:false\">Given the tree:\r\n        4\r\n       \/ \\\r\n      2   7\r\n     \/ \\\r\n    1   3\r\n\r\nAnd the value to search: 2\r\n<\/pre>\n<p>You should return this subtree:<\/p>\n<pre class=\"crayon:false\">      2     \r\n     \/ \\   \r\n    1   3\r\n<\/pre>\n<p>In the example above, if we want to search the value\u00a0<code>5<\/code>, since there is no node with value\u00a0<code>5<\/code>, we should return\u00a0<code>NULL<\/code>.<\/p>\n<h1>Solution: Recursion<\/h1>\n<p>Time complexity: O(logn ~ n)<\/p>\n<p>Space complexity: O(logn ~ n)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 24 ms\r\nclass Solution {\r\npublic:\r\n  TreeNode* searchBST(TreeNode* root, int val) {\r\n    if (root == nullptr) return nullptr;\r\n    if (val == root-&gt;val) \r\n      return root;\r\n    else if (val &gt; root-&gt;val) \r\n      return searchBST(root-&gt;right, val);\r\n    return searchBST(root-&gt;left, val);\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the&#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,222,42],"class_list":["post-3092","post","type-post","status-publish","format-standard","hentry","category-tree","tag-bst","tag-easy","tag-search","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3092","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=3092"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3092\/revisions"}],"predecessor-version":[{"id":3094,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3092\/revisions\/3094"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}