Count Complete Tree Nodes

来源:互联网 发布:淘宝卖家找货源网 编辑:程序博客网 时间:2024/05/14 03:29
class Solution {
public:
    int countNodes(TreeNode* root) {
        if(root==NULL) return 0;
        int left=0,right=0;
        TreeNode* l=root, *r=root;
        while(l) {left++;l=l->left;}                   //如果局部有满二叉树,则直接返回个数;
        while(r) {right++;r=r->right;}
        if(left==right) return pow(2,left)-1;             
        return 1+countNodes(root->left)+countNodes(root->right);
        
    }
};
0 0