|
| 1 | +# [222. Count Complete Tree Nodes (Medium)](https://leetcode.com/problems/count-complete-tree-nodes/) |
| 2 | + |
| 3 | +<p>Given a <b>complete</b> binary tree, count the number of nodes.</p> |
| 4 | + |
| 5 | +<p><b>Note: </b></p> |
| 6 | + |
| 7 | +<p><b><u>Definition of a complete binary tree from <a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a>:</u></b><br> |
| 8 | +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> nodes inclusive at the last level h.</p> |
| 9 | + |
| 10 | +<p><strong>Example:</strong></p> |
| 11 | + |
| 12 | +<pre><strong>Input:</strong> |
| 13 | + 1 |
| 14 | + / \ |
| 15 | + 2 3 |
| 16 | + / \ / |
| 17 | +4 5 6 |
| 18 | + |
| 19 | +<strong>Output:</strong> 6</pre> |
| 20 | + |
| 21 | + |
| 22 | +**Related Topics**: |
| 23 | +[Binary Search](https://leetcode.com/tag/binary-search/), [Tree](https://leetcode.com/tag/tree/) |
| 24 | + |
| 25 | +**Similar Questions**: |
| 26 | +* [Closest Binary Search Tree Value (Easy)](https://leetcode.com/problems/closest-binary-search-tree-value/) |
| 27 | + |
| 28 | +## Solution 1. |
| 29 | + |
| 30 | +Given a subtree, we just compute the lengths of the leftmost path and the rightmost path. |
| 31 | + |
| 32 | +* if they are the same, then this subtree is complete and its node count is `2^length - 1`. |
| 33 | +* otherwise, we recursively count nodes for the left subtree and the right subtree and return the sum of them plus 1. |
| 34 | + |
| 35 | +```cpp |
| 36 | +// OJ: https://leetcode.com/problems/count-complete-tree-nodes/ |
| 37 | +// Author: github.com/lzl124631x |
| 38 | +// Time: O(H^2) |
| 39 | +// Space: O(H) |
| 40 | +class Solution { |
| 41 | + int countLeft(TreeNode *root) { |
| 42 | + int cnt = 0; |
| 43 | + for (; root; ++cnt, root = root->left); |
| 44 | + return cnt; |
| 45 | + } |
| 46 | + int countRight(TreeNode *root) { |
| 47 | + int cnt = 0; |
| 48 | + for (; root; ++cnt, root = root->right); |
| 49 | + return cnt; |
| 50 | + } |
| 51 | +public: |
| 52 | + int countNodes(TreeNode* root) { |
| 53 | + if (!root) return 0; |
| 54 | + int left = countLeft(root), right = countRight(root); |
| 55 | + if (left == right) return (1 << left) - 1; |
| 56 | + return countNodes(root->left) + countNodes(root->right) + 1; |
| 57 | + } |
| 58 | +}; |
| 59 | +``` |
| 60 | +
|
| 61 | +## Solution 2. |
| 62 | +
|
| 63 | +Minor optimization which prevents us from recomputing the lengths that we've already know. |
| 64 | +
|
| 65 | +```cpp |
| 66 | +// OJ: https://leetcode.com/problems/count-complete-tree-nodes/ |
| 67 | +// Author: github.com/lzl124631x |
| 68 | +// Time: O(H^2) |
| 69 | +// Space: O(H) |
| 70 | +class Solution { |
| 71 | + int countLeft(TreeNode *root) { |
| 72 | + int cnt = 0; |
| 73 | + for (; root; ++cnt, root = root->left); |
| 74 | + return cnt; |
| 75 | + } |
| 76 | + int countRight(TreeNode *root) { |
| 77 | + int cnt = 0; |
| 78 | + for (; root; ++cnt, root = root->right); |
| 79 | + return cnt; |
| 80 | + } |
| 81 | + int count(TreeNode* root, int left = INT_MIN, int right = INT_MIN) { |
| 82 | + if (!root) return 0; |
| 83 | + if (left == INT_MIN) left = countLeft(root); |
| 84 | + if (right == INT_MIN) right = countRight(root); |
| 85 | + if (left == right) return (1 << left) - 1; |
| 86 | + return count(root->left, left - 1, INT_MIN) + 1 + count(root->right, INT_MIN, right - 1); |
| 87 | + } |
| 88 | +public: |
| 89 | + int countNodes(TreeNode* root) { |
| 90 | + return count(root); |
| 91 | + } |
| 92 | +}; |
| 93 | +``` |
0 commit comments