/**
* 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 || LeetCode1的更多相关文章

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

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

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

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

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

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

  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. LeetCode Count Complete Tree Nodes

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

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

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

  8. Count Complete Tree Nodes

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

  9. Java for LeetCode 222 Count Complete Tree Nodes

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

随机推荐

  1. android6.0 适配的问题——activity销毁的问题

    1.最近我去运行我们公司所开发的APP,发现出现很多问题,就是从前一个页面跳到另外一个页面后,前一个页面会被销毁. 正常来说,activity跳转过程是这样: A: 存在intent +setActi ...

  2. HTML5 本地存储 localStorage、sessionStorage 的遍历、存储大小限制处理

    HTML5 的本地存储 API 中的 localStorage 与 sessionStorage 在使用方法上是相同的,区别在于 sessionStorage 在关闭页面后即被清空,而 localSt ...

  3. Word2Vec源码解析

    Reference:http://blog.csdn.net/itplus/article/details/37969519  (Word2Vec解析(部分有错)) 源码:http://pan.bai ...

  4. CentOS6.4 安装SSDB

    1.安装 wget --no-check-certificate https://github.com/ideawu/ssdb/archive/master.zipunzip mastercd ssd ...

  5. Jquery实现下拉联动表单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. jquery 操作iframe的几种方法总结

    iframe在复合文档中经常用到,利用jquery操作iframe可以大幅提高效率,这里收集一些基本操作 DOM方法: 父窗口操作IFRAME:window.frames["iframeSo ...

  7. Android -- 闹钟服务的使用(启动与停止)

    1. 效果图

  8. poi excel导出,下载

    poi.jar包 public void downExcel(HttpServletResponse response,Page<ShopApply> page) throws Excep ...

  9. [LintCode] Sort Integers II 整数排序之二

    Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(n ...

  10. [LintCode] Paint House 粉刷房子

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...