{"id":9065,"date":"2021-12-07T20:44:30","date_gmt":"2021-12-08T04:44:30","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9065"},"modified":"2021-12-11T15:00:31","modified_gmt":"2021-12-11T23:00:31","slug":"leetcode-222-count-complete-tree-nodes","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-222-count-complete-tree-nodes\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 222. Count Complete Tree Nodes"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 222. Count Complete Tree Nodes - \u5237\u9898\u627e\u5de5\u4f5c EP395\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/dtLIe1rHYPg?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given the&nbsp;<code>root<\/code>&nbsp;of a&nbsp;<strong>complete<\/strong>&nbsp;binary tree, return the number of the nodes in the tree.<\/p>\n\n\n\n<p>According to&nbsp;<strong><a href=\"http:\/\/en.wikipedia.org\/wiki\/Binary_tree#Types_of_binary_trees\" target=\"_blank\" rel=\"noreferrer noopener\">Wikipedia<\/a><\/strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>2<sup>h<\/sup><\/code>&nbsp;nodes inclusive at the last level&nbsp;<code>h<\/code>.<\/p>\n\n\n\n<p>Design an algorithm that runs in less than&nbsp;<code>O(n)<\/code>&nbsp;time complexity.<\/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\/2021\/01\/14\/complete.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [1,2,3,4,5,6]\n<strong>Output:<\/strong> 6\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = []\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [1]\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The number of nodes in the tree is in the range&nbsp;<code>[0, 5 * 10<sup>4<\/sup>]<\/code>.<\/li><li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4<\/sup><\/code><\/li><li>The tree is guaranteed to be&nbsp;<strong>complete<\/strong>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-1.png\" alt=\"\" class=\"wp-image-9118\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-2.png\" alt=\"\" class=\"wp-image-9119\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-3.png\" alt=\"\" class=\"wp-image-9120\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-3.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-3-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-3-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-4.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-4.png\" alt=\"\" class=\"wp-image-9121\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-4.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-4-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-4-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-5.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-5.png\" alt=\"\" class=\"wp-image-9122\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-5.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-5-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-5-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-56.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-56.png\" alt=\"\" class=\"wp-image-9123\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-56.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-56-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/12\/222-ep395-56-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n\n\n<p><br>For each node, count the height of it&#8217;s left and right subtree by going left only.<\/p>\n\n\n\n<p>Let L = height(left) R = height(root),  if L == R, which means the left subtree is perfect.<br>It has (2^L &#8211; 1) nodes, +1 root, we only need to count nodes of right subtree recursively.<br>If L != R, L must be R + 1 since the tree is complete, which means the right subtree is perfect.<br>It has (2^(L-1) &#8211; 1) nodes, +1 root, we only need to count nodes of left subtree recursively.<\/p>\n\n\n\n<p>Time complexity: T(n) = T(n\/2) + O(logn) = O(logn*logn)<\/p>\n\n\n\n<p>Space complexity: O(logn)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int countNodes(TreeNode* root) {\n    if (root == nullptr) return 0;\n    int l = depth(root->left);\n    int r = depth(root->right);\n    if (l == r) \n      return (1 << l) + countNodes(root->right);\n    else\n      return (1 << (l - 1)) + countNodes(root->left);\n  }\nprivate:\n  int depth(TreeNode* root) {\n    if (root == nullptr) return 0;\n    return 1 + depth(root->left);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given the&nbsp;root&nbsp;of a&nbsp;complete&nbsp;binary tree, return the number of the nodes in the tree. According to&nbsp;Wikipedia, every level, except possibly the last, is completely filled in&#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":[17,28],"class_list":["post-9065","post","type-post","status-publish","format-standard","hentry","category-tree","tag-recursion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9065","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=9065"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9065\/revisions"}],"predecessor-version":[{"id":9124,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9065\/revisions\/9124"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}