-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path222Count Complete Tree Nodes.cpp
36 lines (32 loc) · 1.14 KB
/
222Count Complete Tree Nodes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*************************************************************************
> File Name: 222.cpp
> Author:
> Mail:
> Created Time: Fri Jul 21 10:55:58 2017
************************************************************************/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int countNodes(TreeNode* root) {
if(!root) return 0;
int lh = height(root->left);
int rh = height(root->right);
if(lh == rh)
return (1 << lh) + countNodes(root->right); /*1(根节点) + (1<<lh)-1(完全左子树) + # of rightNode */
else
return (1 << rh) + countNodes(root->left); /*1(根节点) + (1<<rh)-1(完全右子树) + # of leftNode*/
}
private:
int height(TreeNode *root){ //get the height of a complete binary tree.
if(!root) return 0;
return 1 + height(root->left);
}
};