/** * Definition for a binary tree node. * struct TreeNode {* int val; * struct TreeNode *left; * struct TreeNode *right; *}; */#define MAX 1000int 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;}
Count Complete Tree Nodes | | LeetCode1