leetcode_222 Count Complete Tree Nodes
题目:
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.
思路1:
暴力解决,遍历树的所有节点,依次统计。
会超时,题目限定了该二叉树是完全二叉树,该种思路适用于所有二叉树。
思路2:
暴力解决无法测试通过,此时需要考虑完全二叉树的特点。
完全二叉树,至少有一课子树是完美二叉树,另一课子树至少是完全二叉树。
因此可以递归的计算。
判断完美二叉树:
最内侧的叶子结点和最外侧的叶子结点是否在同一层。
代码:
public static int countNodes(TreeNode root){
if(root == null){
return 0;
}
int hl = getLeft(root)+1;
int hr = getRight(root)+1;
if(hl == hr){
int num = (2<<(hl-1))-1;
return num;
}else{
return countNodes(root.left)+countNodes(root.right)+1;
}
}
public static int getRight(TreeNode root) {
int cou = 0;
TreeNode ptr = root;
while(ptr.right != null){
cou++;
ptr = ptr.right;
}
return cou;
}
public static int getLeft(TreeNode root) {
int cou = 0;
TreeNode ptr = root;
while(ptr.left != null){
cou++;
ptr = ptr.left;
}
return cou;
}
leetcode_222 Count Complete Tree Nodes的更多相关文章
- leetcode面试准备:Count Complete Tree Nodes
1 题目 Given a complete binary tree, count the number of nodes. In a complete binary tree every level, ...
- leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
- 完全二叉树的节点个数 Count Complete Tree Nodes
2018-09-25 16:36:25 问题描述: 问题求解: 单纯遍历了一遍,emmm,果然TLE. 解题思路就是比较左边树高度和右边树高度,如果相等,那么就是一个满二叉树,返回1 << ...
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
- 【刷题-LeetCode】222. Count Complete Tree Nodes
Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. Note: Definition ...
- LeetCode Count Complete Tree Nodes
原题链接在这里:https://leetcode.com/problems/count-complete-tree-nodes/ Given a complete binary tree, count ...
- [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- Java for LeetCode 222 Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
随机推荐
- Unity5.x版本AssetBundle打包研究
Unity5的AssetBundle打包机制和以前版本不太一样.简单的说就是,只要给你要打包的资源设置一个AssetBundleName ,Unity自身会对这些设置了名字的资源进行打包,如果一个资源 ...
- zip ubuntu使用
http://www.cnblogs.com/daizhuacai/p/3174885.html 安装: sudo apt-get install zip 解压: unzip -d path file ...
- DBLink创建 ORA-12154: TNS: 无法解析指定的连接标识符
因为对oracle不了解,这个问题可TM的搞了好久! 走的弯路: 1. 在客服端的PLSQL连接工具上折腾,而不是在服务器的PLSQL解决 2. 配置的tnsnames.org文件在环境变量path( ...
- PHP的学习--cookie和session--来自copy_02
PHP的学习--cookie和session 最近读了一点<PHP核心技术与最佳实践>,看了cookie和session,有所收获,结合之前的认识参考了几篇博客,总结一下-- 1. P ...
- HTTP 战役 与 历史
导火线1992年,有一家公司Nombas 开发了一种叫C--的嵌入式脚本语言,后来觉得名字比较晦气,最终改名为scriptEase.而这种可以嵌入网页中的脚本的理念,成为日后移动互联网蓬勃发展的一块重 ...
- 笔记本(WIN7|XP)发射wifi信号 当无线路由使用
Windows7系统 第一步:是你的电脑上要有两个网卡一个有线一个无线(笔记本应该都有)台式机可以考虑买个USB无线网卡(50左右) 第二步:win+R CMD 输入(只输入红色部分) netsh ...
- The Earth Mover's Distance
The EMD is based on the minimal cost that must be paid to transform one distribution into the other. ...
- 15.2 THE USE OF A LARGE REGISTER FILE
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION
- 12.162s 1805.867s
[SQL]DROP PROCEDURE IF EXISTS truncate_insert_sales_rank_toparow_week; 受影响的行: 时间: .001s [SQL] CREATE ...
- Java 集合快速失败异常
快速失败 在JDK中,查看集合有很多关于快速失败的描述: 注意,此实现不是同步的.如果多个线程同时访问一个哈希映射,而其中至少一个线程从结构上修改了该映射,则它必须 保持外部同步.(结构上的修改是指添 ...