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

  

Count Complete Tree Nodes || LeetCode的更多相关文章

  1. Count Complete Tree Nodes ——LeetCode

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  2. leetcode面试准备:Count Complete Tree Nodes

    1 题目 Given a complete binary tree, count the number of nodes. In a complete binary tree every level, ...

  3. leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes

    完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...

  4. 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)

    [LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...

  5. 【刷题-LeetCode】222. Count Complete Tree Nodes

    Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. Note: Definition ...

  6. 完全二叉树的节点个数 Count Complete Tree Nodes

    2018-09-25 16:36:25 问题描述: 问题求解: 单纯遍历了一遍,emmm,果然TLE. 解题思路就是比较左边树高度和右边树高度,如果相等,那么就是一个满二叉树,返回1 << ...

  7. LeetCode Count Complete Tree Nodes

    原题链接在这里:https://leetcode.com/problems/count-complete-tree-nodes/ Given a complete binary tree, count ...

  8. [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  9. [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...

随机推荐

  1. BZOJ2320 : 最多重复子串

    本题就是求重复数最多的字典序最小的$runs$,如果重复数为1,那么做法显然,然后只考虑重复数大于1的情况. 从小到大枚举长度$len$,对于每个关键点$x=i\times len$,有且仅有一个长度 ...

  2. BZOJ3072 : [Pa2012]Two Cakes

    考虑DP,设$f[i][j]$表示考虑了$a[1..i]$和$b[1..j]$的最小代价. 若$a[i]==b[j]$,则$f[i][j]=\min(f[i-1][j],f[i][j-1])+1$. ...

  3. document.forms[0].submit object is not a function

    今天在做项目的时候发现了一个问题:document.forms[0].submit object is not a function. 这个问题是在用JavaScript 代码来提交一个表单时发生的. ...

  4. java.io包详细解说

    转自:http://hzxdark.iteye.com/blog/40133 hzxdark的博客 我不知道各位是师弟师妹们学java时是怎样的,就我的刚学java时的感觉,java.io包是最让我感 ...

  5. Leetcode Sqrt(x)

    参考Babylonian method  (x0  越接近S的平方根越好) class Solution { public: int sqrt(double x) { ) ; , tolerance ...

  6. BZOJ3456: 城市规划

    Description 刚刚解决完电力网络的问题, 阿狸又被领导的任务给难住了. 刚才说过, 阿狸的国家有n个城市, 现在国家需要在某些城市对之间建立一些贸易路线, 使得整个国家的任意两个城市都直接或 ...

  7. Crystal Reports 2008(水晶报表) 第一个报表

    学习Craystal Reports 2008的时候,光看说明文档,很多东西看了就忘了. 我在看文档的时候,是跟着文档上面来做的. 这样边看边做,效果还不错哈 下面就是我的第一个demo 先看看效果: ...

  8. 通过hibernate session.connection()获得数据库连接时,导致的查询缓慢甚至假死机问题

    在使用hibernate的应用中,如果需要直接使用Java.sql.Connection,一般我们是通过hibernate的session.connection()获得的,然后session.clos ...

  9. PHP 解决nginx 用file_get_content 问题

    $my_curl = curl_init(); //初始化一个curl对象 curl_setopt($my_curl, CURLOPT_URL, "http://www.webjoy.net ...

  10. JDBC学习笔记2

    数据库数据更新操作statement接口下操作:[代码] import java.sql.DriverManager;import java.sql.SQLException; public clas ...