http://www.itint5.com/oj/#4

这题是利用完全二叉树的性质计算节点数目。那么是通过比较左右子树的最左结点的高度来看那边是满的,然后递归计算。

//使用getLeftChildNode(TreeNode)获得左儿子结点
//使用getRightChildNode(TreeNode)获得右儿子结点
//使用isNullNode(TreeNode)判断结点是否为空
int get_left_height(TreeNode root) {
if (isNullNode(root)) {
return 0;
} else {
return get_left_height(getLeftChildNode(root)) + 1;
}
} int count_complete_binary_tree_nodes(TreeNode root) {
if (isNullNode(root))
return 0;
TreeNode leftNode = getLeftChildNode(root);
TreeNode rightNode = getRightChildNode(root);
int lheight = get_left_height(leftNode);
int rheight = get_left_height(rightNode);
if (lheight == rheight) {
return (1 << lheight) + count_complete_binary_tree_nodes(rightNode);
} else {
return (1 << rheight) + count_complete_binary_tree_nodes(leftNode);
}
}

  

[itint5]完全二叉树节点个数的统计的更多相关文章

  1. 先序遍历创建二叉树,对二叉树统计叶子节点个数和统计深度(创建二叉树时#代表空树,序列不能有误)c语言

    #include "stdio.h" #include "string.h" #include "malloc.h" #define NUL ...

  2. 222. Count Complete Tree Nodes -- 求完全二叉树节点个数

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

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

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

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

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

  5. [Swift]LeetCode222. 完全二叉树的节点个数 | Count Complete Tree Nodes

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

  6. PAT甲题题解-1004. Counting Leaves (30)-统计每层叶子节点个数+dfs

    统计每层的叶子节点个数建树,然后dfs即可 #include <iostream> #include <cstdio> #include <algorithm> # ...

  7. LeetCode 222.完全二叉树的节点个数(C++)

    给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置.若最底 ...

  8. 已知一棵完全二叉树,求其节点的个数 要求:时间复杂度低于O(N),N为这棵树的节点个数

    package my_basic.class_4; public class Code_08_CBTNode { // 完全二叉树的节点个数 复杂度低于O(N) public static class ...

  9. Leetcode 222.完全二叉树的节点个数

    完全二叉树的节点个数 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最 ...

随机推荐

  1. (转)关于redis、memcache、mongoDB 的对比

    从以下几个维度,对redis.memcache.mongoDB 做了对比,欢迎拍砖 1.性能 都比较高,性能对我们来说应该都不是瓶颈 总体来讲,TPS方面redis和memcache差不多,要大于mo ...

  2. Linux下Tomcat启动正常,但浏览器无法访问

    1.服务器可ping通 2.服务器抓本地的http请求包,可以抓到 3.本地抓服务器返回的http响应包,抓不到 经过查找,是由于开启了Linux防火墙 查看防火墙配置(需要root权限) [root ...

  3. CSS 绝对定位和相对定位

    CSS定位属性:一个定位属性,需配合四个定位坐标,实现定位 固定定位fixed 说明: 1.固定定位是相对于"浏览器窗口" 2.如果只设置了定位属性,未指定定位坐标时,元素将停留在 ...

  4. Poj OpenJudge 百练 2632 Crashing Robots

    1.Link: http://poj.org/problem?id=2632 http://bailian.openjudge.cn/practice/2632/ 2.Content: Crashin ...

  5. C++ 数组名作为函数参数 都是我的错

    ]) { cout<<sizeof(arr); } 这样一道题,我以为输出的是100呢……32位系统,结果是4 因为:数组名在函数体中被当成一个指针来使用 #include <ios ...

  6. java-多线程-join函数

    join()>>不带参数 线程A调用线程B.join,意思就是线程A并入了线程B,当执行完线程B,再去执行线程A后续动作 join(int keepTims)>>带参数,与上面 ...

  7. Hash function

    Hash function From Wikipedia, the free encyclopedia   A hash function that maps names to integers fr ...

  8. linux的SVN搭建与同步

    以下的配置文件,开头都不能有空格 安装: yum install subversion 验证:svnserve --version 代码库创建:(类似,可以建立多个仓库)[repo]mkdir -p ...

  9. 浅谈strtok

    原型:char *strtok(char *s, char *delim); 功能:分解字符串为一组标记串.s为要分解的字符串,delim为分隔符字符串. 说明:首次调用时,s必须指向要分解的字符串, ...

  10. Thinkcmf 在新浪云上的部署问题

    最近要开发一个社团主页,于是想到了CMF内容管理系统的,但是直接在自己的服务器测试成本太高,于是选择了在新浪云上进行部署测试. 但是在安装Thinkcmf的过程中产生了一些技术性的问题.但最后终于在自 ...