/** * 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; }