[itint5]完全二叉树节点个数的统计
这题是利用完全二叉树的性质计算节点数目。那么是通过比较左右子树的最左结点的高度来看那边是满的,然后递归计算。
//使用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]完全二叉树节点个数的统计的更多相关文章
- 先序遍历创建二叉树,对二叉树统计叶子节点个数和统计深度(创建二叉树时#代表空树,序列不能有误)c语言
#include "stdio.h" #include "string.h" #include "malloc.h" #define NUL ...
- 222. Count Complete Tree Nodes -- 求完全二叉树节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- [Swift]LeetCode222. 完全二叉树的节点个数 | Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- PAT甲题题解-1004. Counting Leaves (30)-统计每层叶子节点个数+dfs
统计每层的叶子节点个数建树,然后dfs即可 #include <iostream> #include <cstdio> #include <algorithm> # ...
- LeetCode 222.完全二叉树的节点个数(C++)
给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置.若最底 ...
- 已知一棵完全二叉树,求其节点的个数 要求:时间复杂度低于O(N),N为这棵树的节点个数
package my_basic.class_4; public class Code_08_CBTNode { // 完全二叉树的节点个数 复杂度低于O(N) public static class ...
- Leetcode 222.完全二叉树的节点个数
完全二叉树的节点个数 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最 ...
随机推荐
- Eclipse JavaEE设置内置浏览器和外部浏览器
Eclipse JavaEE设置内置浏览器和外部浏览器 我们在使用Java EE版本的Eclipse开发Java Web程序时,Eclipse会有一个默认的内置浏览器查看网页的效果,如下图 但是内置浏 ...
- Java流的正确关闭方式
因为流是无论如何一定要关闭的,所以要写在finally里.如下: BufferedReader reader = null; try { reader = (BufferedReader) getRe ...
- Java使用泛型类来提高方法的可重用性
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3832268.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- Asp.net Response.Redirect with post data
string url = String.Format("{0}://{1}/{2}", Request.Url.Scheme, Request.Url.Authority, &qu ...
- sql语句分组统计出年月日下数据记录数目
把时间设置date类型,其中 select count(*),year(b.date),month(b.date) ,day(b.date) from person as b group by yea ...
- 解决Ajax跨域问题:Origin xx is not allowed by Access-Control-Allow-Origin.
一:使用jsonp格式, 如jquery中ajax请求参数 dataType:'JSONP'. <html> <head> <title>title</t ...
- php curl 伪造IP来源的代码分享
php curl 可以模仿用户登录,还可以模仿用户IP地址.伪造IP来源. 1,curl发出请求的文件fake_ip.php: <?php $ch = curl_init(); $url = & ...
- CodeIgniter 框架---学习笔记
1.输出sql语句:echo $this->db->last_query();
- PHP学习之开发工具
刚接触PHP,必然需要一套完整的开发工具.每个语言都有各种各样的编辑工具.采用了相对来说比较了解的Eclipse来作为开发工具. 1.要是用Eclipse需要安装JDK或JRE(Eclipse本身就是 ...
- 实例讲解如何在Delphi中动态创建dxBarManager内容
一.dxBarManager中一些非常重要的概念: TCategorys:为了方便对dxBarManager中的项目进行归类而设计的一个属性,当然,只使用默认的名字为Default的Category也 ...