{"id":4464,"date":"2018-12-18T14:59:02","date_gmt":"2018-12-18T22:59:02","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4464"},"modified":"2018-12-18T15:28:10","modified_gmt":"2018-12-18T23:28:10","slug":"leetcode-958-check-completeness-of-a-binary-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-958-check-completeness-of-a-binary-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 958. Check Completeness of a Binary Tree"},"content":{"rendered":"\n<p>Given a binary tree, determine if it is a&nbsp;<em>complete binary tree<\/em>.<\/p>\n\n\n\n<p><strong>Definition of a complete binary tree from&nbsp;<a href=\"http:\/\/en.wikipedia.org\/wiki\/Binary_tree#Types_of_binary_trees\" target=\"_blank\" rel=\"noreferrer noopener\">Wikipedia<\/a>:<\/strong><br>In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2<sup>h<\/sup>&nbsp;nodes inclusive at the last level h.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2018\/12\/15\/complete-binary-tree-1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"crayon:false\"><strong>Input: <\/strong>[1,2,3,4,5,6]<br><strong>Output: <\/strong>true<br><strong>Explanation: <\/strong>Every level before the last is full (ie. levels with node-values {1} and {2, 3}), and all nodes in the last level ({4, 5, 6}) are as far left as possible.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2018\/12\/15\/complete-binary-tree-2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[1,2,3,4,5,null,7]<br><strong>Output: <\/strong>false<br><strong>Explanation: <\/strong>The node with value 7 isn't as far left as possible.<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The tree will have between 1 and 100 nodes.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution:<\/strong><\/h2>\n\n\n\n<p>Level order traversal, if any nodes appears after a missing node then the tree is not a perfect binary tree.<\/p>\n\n\n\n<p>Time complexity: O(n)<\/p>\n\n\n\n<p>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"wp-block-code\">class Solution {\npublic:\n  bool isCompleteTree(TreeNode* root) {\n    if (!root) return true;\n    queue<TreeNode> q;\n    q.push(root);\n    bool missing = false;\n    while (!q.empty()) {\n      auto size = q.size();      \n      while (size--) {\n        auto node = q.front(); q.pop();        \n        if (node) {\n          if (missing) return false;\n          q.push(node-&gt;left);\n          q.push(node-&gt;right);\n        } else {\n          missing = true;\n        }\n      }      \n    }\n    return true;\n  }\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"wp-block-code\">class Solution:\n  def isCompleteTree(self, root):\n    if not root: return True\n    q = collections.deque([root])\n    missing = False\n    while q:\n      size = len(q)    \n      while size > 0:\n        size -= 1\n        node = q.popleft()\n        if node:\n          if missing: return False\n          q.append(node.left)\n          q.append(node.right)\n        else:\n          missing = True\n    return True\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary tree, determine if it is a&nbsp;complete binary tree. Definition of a complete binary tree from&nbsp;Wikipedia:In a complete binary tree every level, except&#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":[312,177,28],"class_list":["post-4464","post","type-post","status-publish","format-standard","hentry","category-tree","tag-balance","tag-medium","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4464","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=4464"}],"version-history":[{"count":11,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4464\/revisions"}],"predecessor-version":[{"id":4483,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4464\/revisions\/4483"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}