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. SQL通过日期计算年龄

    首先建立一个表如下: ======================= BirthDay datetime not null Age 通过公式计算得出 ======================= 以 ...

  2. Linux 查看系统版本及位数

    1. 查看内核版本命令: 1) [root@www ~]# cat /proc/version    Linux version 2.6.9-22.ELsmp (bhcompile@crowe.dev ...

  3. shell命令行快速编辑命令

    ctrl r:命令行出现 reverse-i-search,输入字符将在输入历史中匹配命令 ctrl p:向前翻看历史 ctrl n:向后翻看历史 ctrl a:命令行首 ctrl e:命令行尾 ct ...

  4. 单纯形法C++实现

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4156685.html 使用单纯型法来求解线性规划,输入单纯型法的松弛形式,是一个大矩阵,第一 ...

  5. PHP将二进制文件存入数据库以及从数据库中读取二进制文件

    <?php $file = 'abcd.sqlite'; mysql_connect('localhost','root','123456'); mysql_select_db('zblog') ...

  6. JAVA对象是如何占用内存的

      本文使用的是32位的JVM ,jdk1.6.本文基本是翻译的,加上了一些自己的理解,原文见文章底下链接.     在本文中,我们讨论如何计算或者估计一个JAVA对象占多少内存空间.(注意,使用 C ...

  7. 《C和指针》 读书笔记 -- 第10章 结构和联合

    1.聚合数据类型能够同时存储超过一个的单独数据,c提供了两种类型的聚合数据类型,数组和结构. 2.[1] struct SIMPLE { int a; }; struct SIMPLE x; [2] ...

  8. table如何在过宽的时候添加滚动条

    在页面中,往往由于一个table的列过于多,导致页面放不下,然后内容各种挤变形. 这里说一个解决方法,.先用一个DIV把TABLE包围起来.然后给这个DIV设置宽度,并且设置overflow:auto ...

  9. poj 3518 Corporate Identity 后缀数组->多字符串最长相同连续子串

    题目链接 题意:输入N(2 <= N <= 4000)个长度不超过200的字符串,输出字典序最小的最长公共连续子串; 思路:将所有的字符串中间加上分隔符,注:分隔符只需要和输入的字符不同, ...

  10. 实现Linux select IO复用C/S服务器代码

    已在ubuntu 下验证可用 服务器端 #include<stdio.h>#include<unistd.h>#include<stdlib.h>#include& ...