{"id":4163,"date":"2018-10-06T21:29:53","date_gmt":"2018-10-07T04:29:53","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4163"},"modified":"2018-10-06T21:58:37","modified_gmt":"2018-10-07T04:58:37","slug":"leetcode-919-complete-binary-tree-inserter","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-919-complete-binary-tree-inserter\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 919. Complete Binary Tree Inserter"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>A\u00a0<em>complete<\/em>\u00a0binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.<\/p>\n<p>Write a data structure\u00a0<code>CBTInserter<\/code>\u00a0that is initialized with a complete binary tree and supports the following operations:<\/p>\n<ul>\n<li><code>CBTInserter(TreeNode root)<\/code>\u00a0initializes the data structure on a given tree\u00a0with head node\u00a0<code>root<\/code>;<\/li>\n<li><code>CBTInserter.insert(int v)<\/code>\u00a0will insert a\u00a0<code>TreeNode<\/code>\u00a0into the tree with value\u00a0<code>node.val =\u00a0v<\/code>\u00a0so that the tree remains complete,\u00a0<strong>and returns the value of the parent of the inserted\u00a0<code>TreeNode<\/code><\/strong>;<\/li>\n<li><code>CBTInserter.get_root()<\/code>\u00a0will return the head node of the tree.<\/li>\n<\/ul>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>inputs = <span id=\"example-input-1-1\">[\"CBTInserter\",\"insert\",\"get_root\"]<\/span>, inputs = <span id=\"example-input-1-2\">[[[1]],[2],[]]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">[null,1,[1,2]]<\/span>\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>inputs = <span id=\"example-input-2-1\">[\"CBTInserter\",\"insert\",\"insert\",\"get_root\"]<\/span>, inputs = <span id=\"example-input-2-2\">[[[1,2,3,4,5,6]],[7],[8],[]]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">[null,3,4,[1,2,3,4,5,6,7,8]]<\/span><\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li>The initial given tree is complete and contains between\u00a0<code>1<\/code>\u00a0and\u00a0<code>1000<\/code>\u00a0nodes.<\/li>\n<li><code>CBTInserter.insert<\/code>\u00a0is called at most\u00a0<code>10000<\/code>\u00a0times per test case.<\/li>\n<li>Every value of a given or inserted node is between\u00a0<code>0<\/code>\u00a0and\u00a0<code>5000<\/code>.<\/li>\n<\/ol>\n<h1><strong>Solution 2: Deque<\/strong><\/h1>\n<p>Using a deck to keep track of insertable nodes (potential parents) in order.<\/p>\n<p>Time complexity: O(1) \/ O(n) first call<\/p>\n<p>Space complexity: O(n)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\nclass CBTInserter {\r\npublic:\r\n  CBTInserter(TreeNode* root): root_(root), d_({root}) {}\r\n\r\n  int insert(int v) {    \r\n    while (!d_.empty()) {\r\n      TreeNode* r = d_.front();\r\n      if (r-&gt;left &amp;&amp; r-&gt;right) {\r\n        d_.push_back(r-&gt;left);\r\n        d_.push_back(r-&gt;right);\r\n        d_.pop_front();\r\n      } else if (r-&gt;left) {\r\n        r-&gt;right = new TreeNode(v);\r\n        return r-&gt;val;\r\n      } else {\r\n        r-&gt;left = new TreeNode(v);        \r\n        return r-&gt;val;\r\n      }\r\n    }\r\n  }\r\n\r\n  TreeNode* get_root() { return root_; }\r\nprivate:\r\n  std::deque&lt;TreeNode*&gt; d_;\r\n  TreeNode* root_;\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem A\u00a0complete\u00a0binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left&#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":[214,423,28],"class_list":["post-4163","post","type-post","status-publish","format-standard","hentry","category-tree","tag-deque","tag-inserter","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4163","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=4163"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4163\/revisions"}],"predecessor-version":[{"id":4170,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4163\/revisions\/4170"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}