c++ 79ms。perfect tree minus missed nodes


  • 0
    X
    // perfect tree minus missed nodes
    class Solution {
    public:
        int countNodes(TreeNode* root) {
            if( !root ) return 0;
            TreeNode *tmp = root;
            int dep=0;
            while( tmp ) dep++,tmp=tmp->left;
            int ret = (1<<dep)-1;
            for(dep-- ; dep>0 && root; dep--){
                int dr = 0;
                tmp = root->right;
                while( tmp ) dr++,tmp=tmp->left;
                if( dr == dep ){
                    root = root->right;
                }else{
                    ret -= 1<<dr;
                    root = root->left;
                }
            }
            return ret;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.