LeetCode222 Count CompleteTree Nodes(计算全然二叉树的节点数) Java 题解
题目:
Given a complete binary tree, count the number of nodes.
Definition of a complete binary tree from Wikipedia:
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes
inclusive at the last level h.
解题:
假设用常规的解法一个个遍历,就是O(n)时间复杂度 。会不通过,这边就不写O(n)的代码了。由于是全然二叉树,满二叉树有一个性质是节点数等于2^h-1,h为高度,所以能够这样推断节点的左右高度是不是一样,假设是一样说明是满二叉树,就能够用刚才的公式。假设左右不相等就递归计算左右节点。
代码:
public static int countNodes(TreeNode root) {
if(root==null)
return 0;
else {
int left=getLeftHeight(root);
int right=getRightHeight(root);
if(left==right)
return (1<<left)-1;
else {
return countNodes(root.right)+countNodes(root.left)+1;
}
}
}
public static int getRightHeight(TreeNode root) {
int height=0;
while(root!=null)
{
height++;
root=root.left;
}
return height;
}
public static int getLeftHeight(TreeNode root) {
int height=0;
while(root!=null)
{
height++;
root=root.right;
}
return height;
}
LeetCode222 Count CompleteTree Nodes(计算全然二叉树的节点数) Java 题解的更多相关文章
- C++计算二叉树的节点数和高度
用struct结构体的写法: /* * description: 计算二叉树的层数和节点数 * writeby: nick * date: 2012-10-23 16:16 * */ #include ...
- C递归算法与栈的分析,非全然二叉树遍历分析---ShinePans
对于递归,这里面的分析最好当然是用图形的方式来分析了.这里来总结一下 1.首先对于栈的理解: 先进后出,后进先出 先进后出 2.在进行非全然二叉树的存储之后,我们要做的是对其 ...
- 二叉树遍历等基本操作(Java实现)
前中后序遍历递归实现+层序遍历: 树的结点类代码: public class TreeNode<Value extends Comparable<? super Value>> ...
- 李洪强iOS经典面试题35-按层遍历二叉树的节点
李洪强iOS经典面试题35-按层遍历二叉树的节点 问题 给你一棵二叉树,请按层输出其的节点值,即:按从上到下,从左到右的顺序. 例如,如果给你如下一棵二叉树: 3 / \ 9 20 ...
- 求二叉树第K层的节点个数+求二叉树叶子节点的个数
size_t _FindLeafSize(Node* root) //求二叉树叶子节点的个数 { //static size_t count = 0; if ...
- 面试常见二叉树算法题集锦-Java实现
1.求二叉树的深度或者说最大深度 /* ***1.求二叉树的深度或者说最大深度 */ public static int maxDepth(TreeNode root){ if(root==null) ...
- 六:二叉树中第k层节点个数与二叉树叶子节点个数
二叉树中第k层节点个数 递归解法: (1)假设二叉树为空或者k<1返回0 (2)假设二叉树不为空而且k==1.返回1 (3)假设二叉树不为空且k>1,返回左子树中k-1层的节点个数与右子树 ...
- R语言:计算现金工资的币数
新入职的员工,有的没有相应银行卡,需要计算现金工资的币数.实发工资,一般取整数. 简化计算,纸币面值只有100.10.1.4278除以100等于42余78,78除以10等于7余8,8除以1等于8. 复 ...
- LeetCode222——Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
随机推荐
- AsyncTask中各个函数详细的调用过程,初步实现异步任务
AsyncTask内部类可能会产生内存泄露的问题 解决上述内部类可能引起的内存泄露问题的方法 将AsyncTask或者Thread的子类作为单独的类文件,不持有Activity的强引用 将Async ...
- GridView和SimpleAdapter实现网格布局
android:horizontalSpacing 元素之间的水平间距 android:verticalSpacing 元素之间的垂直间距 android:numColumns ...
- LaTeX技巧24:LaTeX常用命令集锦
\hyphenation{word list} %断字命令:\showthe\topmargin %显示某个参数的数值或者内容: 在tex编译过程中出现行溢出(overflow hbox)是由于断字程 ...
- MAT(Memory Analyzer Tool)内存分析工具的使用
开发.应用中老是会遇到OutOfMemory异常,而且常常是过一段时间内存才被吃光,这里可以利用java heap dump出jvm内存镜像,然后再对其进行分析来查找问题. 平常利用jmap -dum ...
- Home Assistant 安装Samba和SSH server 插件
安装 Samba 插件 Samba 是 SMB/CIFS 网络协议的重新实现, 它作为 NFS 的补充使得在 Linux 和 Windows 系统中进行文件共享.打印机共享更容易实现. Smaba 服 ...
- Gas Station leetcode java
题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...
- 【大数据】下载Windows版本的Redis 转
https://www.cnblogs.com/tommy-huang/p/6093813.html 下载Windows版本的Redis 1.打开官网http://redis.io/点击Downl ...
- TSQL:判断某较短字符串在较长字符串中出现的次数。
给定一个较短字符串shortStr='ab',和一个较长字符串longStr='adkdabkwelabwkereabrsdweo2342ablk234lksdfsdf1abe': 判断shortSt ...
- linux kernel内存映射实例分析
作者:JHJ(jianghuijun211@gmail.com)日期:2012/08/24 欢迎转载,请注明出处 引子 现在android智能手机市场异常火热,硬件升级非常迅猛,arm cortex ...
- awk学习 (good)
原文:http://blog.chinaunix.net/uid-23302288-id-3785105.html awk是行处理器: 相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓 ...