zoukankan      html  css  js  c++  java
  • Count Complete Tree Nodes || LeetCode

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    #define MAX 1000
    int Path[MAX];
    int index1;
    
    int power(int x,int n){
        int i,re;
        if(n==0)return 1;
        for(i=0,re=1;i<n;++i){
            re=re*x;
        }
        return re;
    }
    
    void find_path(struct TreeNode *root){
        if( (root->left==NULL&&root->right==NULL) || (root==NULL) )return  ;
        struct TreeNode *t1,*t2;
        t1=root->left,t2=root->right;
        while(t1&&t2){
            t1 = t1->left;
            t2 = t2->left;
        }
        if(t1){
            Path[index1++]=0;
            find_path(root->left);
        }
        else {
            Path[index1++]=1;
            find_path(root->right);
        }
    }
    
    int countNodes(struct TreeNode* root) {
        int  nlevel,i,temp,start,end;
        
        if(root==NULL)return 0;
        if(root->left==NULL && root->right==NULL) return 1;
        index1=0;
        find_path(root);
        nlevel=power(2,index1);
        for(i=0,start=1,end=nlevel;i<index1;++i)
        {
            if(Path[i]==0)
                end=(end+start)/2;
            else
                start=(end+start)/2+1;
            if (end==start)break;
        }
        return nlevel-1+start;
    }
    

      

  • 相关阅读:
    as
    留言板
    HEOI2020游记
    min_25筛学习笔记
    计算几何初步
    「狗屁不会」exlucas
    GCD of Sequence
    做题记录
    一些奇怪的坑+好东西
    关于我
  • 原文地址:https://www.cnblogs.com/ProtectedDream/p/4565757.html
Copyright © 2011-2022 走看看