题目:
分析1:递归:
/**
* 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==NULL) return 0;
return 1+countNodes(root->right)+countNodes(root->left);
}
};
分析2:题解中给出了一种二分的思路。
二分时,进行的最后一行的二分,结合公式来进行计算一共有多少个。
最后一层如何进行二分呢?
1.先左,然后右右右右右右右右右右
2.先右,然后左左左左左左左左左左