{"id":3099,"date":"2018-07-12T20:42:16","date_gmt":"2018-07-13T03:42:16","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3099"},"modified":"2018-07-12T21:30:51","modified_gmt":"2018-07-13T04:30:51","slug":"leetcode-701-insert-into-a-binary-search-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-701-insert-into-a-binary-search-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 701. Insert into 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 to be inserted into the tree,\u00a0insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.<\/p>\n<p>Note that there may exist\u00a0multiple valid ways for the\u00a0insertion, as long as the tree remains a BST after insertion. You can return any of them.<\/p>\n<p>For example,<\/p>\n<pre>Given the tree:\r\n        4\r\n       \/ \\\r\n      2   7\r\n     \/ \\\r\n    1   3\r\nAnd the value to insert: 5\r\n<\/pre>\n<p>You can return this binary search tree:<\/p>\n<pre>         4\r\n       \/   \\\r\n      2     7\r\n     \/ \\   \/\r\n    1   3 5\r\n<\/pre>\n<p>This tree is also valid:<\/p>\n<pre class=\"\">         5\r\n       \/   \\\r\n      2     7\r\n     \/ \\   \r\n    1   3\r\n         \\\r\n          4<\/pre>\n<h1><strong>Solution: Recursion<\/strong><\/h1>\n<p>Time complexity: O(logn ~ n)<\/p>\n<p>Space complexity: O(logn ~ n)<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 52 ms\r\nclass Solution {\r\npublic:\r\n  TreeNode* insertIntoBST(TreeNode* root, int val) {\r\n    if (root == nullptr) return new TreeNode(val);\r\n    if (val &gt; root-&gt;val)\r\n      root-&gt;right = insertIntoBST(root-&gt;right, val);\r\n    else\r\n      root-&gt;left = insertIntoBST(root-&gt;left, val);\r\n    return root;\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 to be inserted into the tree,\u00a0insert the value into the BST.&#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,324,177,28],"class_list":["post-3099","post","type-post","status-publish","format-standard","hentry","category-tree","tag-bst","tag-insert","tag-medium","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3099","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=3099"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3099\/revisions"}],"predecessor-version":[{"id":3101,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3099\/revisions\/3101"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}