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. OC6_目录及文件的创建

    // // main.m // OC6_目录及文件的创建 // // Created by zhangxueming on 15/6/19. // Copyright (c) 2015年 zhangx ...

  2. javascript笔记——cookie解析

    JavaScript中的另一个机制:cookie,则可以达到真正全局变量的要求. cookie是浏览器 提供的一种机制,它将document 对象的cookie属性提供给JavaScript.可以由J ...

  3. 免费的HTML5连载来了《HTML5网页开发实例详解》连载(五)图解通过Fiddler加速开发

    Fiddler是Windows底下最强大的请求代理调试工具,监控任何浏览器的HTTP/HTTPS流量,窜改客户端请求和服务器响应,解密HTTPS Web会话,图4.44为Fiddler原理示意图. 图 ...

  4. 使用WebJar管理css、JavaScript文件

    Web前端使用了越来越多的JS或CSS,如jQuery, Backbone.js 和Bootstrap.一般情况下,我们是将这些Web资源拷贝到Java的目录下,通过手工进行管理,这种通方式容易导致文 ...

  5. python staticmethod classmethod

    http://www.cnblogs.com/chenzehe/archive/2010/09/01/1814639.html classmethod:类方法staticmethod:静态方法 在py ...

  6. 【Delphi】窗体阴影

    procedure TForm1.FormCreate(Sender: TObject); begin SetClassLong(Handle, GCL_STYLE, GetClassLong(Han ...

  7. TImage 的一些操作

    //给 image上写数字. Image1.Picture.Bitmap.Height:= Image1.Height; Image1.Picture.Bitmap.Width:= Image1.Wi ...

  8. 统计网卡TX(发送)RX(接受)流量脚本

    显示网卡流量的方法蛮多,一般我们可以通过dstat来查看,但dstat不一定所有的机器都有安装.而我们知道,通过ifconfig可以看到某一网卡发送与接收的字节数,所以我们可以写一个脚本来统计一下. ...

  9. C语言标记化结构初始化语法

    C语言标记化结构初始化语法 (designated initializer),而且还是一个ISO标准. #include <stdio.h> #include <stdlib.h&g ...

  10. Js 获取当前时间

    Js获取当前日期时间及其它操作 var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整 ...