Skip to content

Latest commit

 

History

History
43 lines (39 loc) · 988 Bytes

#222 Count Complete Tree Nodes.md

File metadata and controls

43 lines (39 loc) · 988 Bytes

#222 Count Complete Tree Nodes

Leetcoed #222

My Solution

Iteration

class Solution {
public:
    int countNodes(TreeNode* root) {
        if(root==NULL) return 0;
        queue<TreeNode*> que;
        que.push(root);
        int count=0;
        while(!que.empty()){
                int size=que.size();
                for(int i=0;i<size;i++){
                        TreeNode *cur=que.front();
                        que.pop();
                        if(cur->left) que.push(cur->left);
                        if(cur->right) que.push(cur->right);
                }
                count+=size;
        }
            return count;
    }
};

Recursion

class Solution {
public:
    int getcount(TreeNode *Node){
            if(Node==NULL) return 0;
            return getcount(Node->left)+getcount(Node->right)+1;
    }
    int countNodes(TreeNode* root) {
       return getcount(root);
    }
};