LeetCode222 Count Complete Tree Nodes
对于一般的二叉树,统计节点数目遍历一遍就可以了,但是这样时间复杂度O(n),一下就被卡住了。
这题首先要明白的是,我们只需要知道叶子节点的数目就能统计出总节点树。
想法1:
既然是完全二叉树,我肯定是从左子树开始看,如果左子树不完整,右子树就不用再遍历了。由此形成一个递归的搜索过程,先搜索左子树,如果不完整,直接停止搜索,统计完毕;否则,还要再搜索右子树。
这样就能避开完全搜索遍历整棵树,但是当树接近满树的时候实际上还是将整颗树遍历了一遍。
想法2:
完全二叉树不同于满树的一点是,我们只能肯定它的最左边那个叶子节点肯定是存在的(这里说存在不太准确,实际就把完全二叉树当满树看,然后那些不满的地方就认为是叶子节点不存在)。所以一颗完全二叉树的高度只需要一路向左就能求得。现在告诉你一颗完全二叉树可能不是满树,从根节点的角度,就要看,到底从哪开始不满的。
按照定义,不满的节点肯定在右边。如果我们求解根节点的右子树的高度=H - 1,说明左子树肯定是满的,不用再看了;否则,右节点高度 = H - 2,说明root->right的叶节点都缺了,右边不用再看了。
有没有发现这种想法使得我们几乎一次排除了一半的元素,所以这题有一个标签叫做binary search,因为感觉想法上很像。
int countNodes(TreeNode* root) {
int height = 0;
auto p = root;
while(p != NULL){
height++;
p = p->left;
}
if(height < 2)
return height;
int countLeaf = 0;
int curHeight = height;
while(root){
auto pr = root->right;
int heightR = 0;
while(pr != NULL){
heightR++;
pr = pr->left;
}
if (heightR == 0){
countLeaf+= 1;
break;
}
else if(heightR == curHeight - 1){
//we don't need to look at the left child any more
//because it must be full
countLeaf += (1 << (heightR - 1));
root = root->right;
}
else{
//we don't need to look at the right child any more
//because there no leaf node
root = root->left;
}
curHeight -= 1;
}
return countLeaf + (1 << (height - 1)) - 1;
}
LeetCode222 Count Complete Tree Nodes的更多相关文章
- LeetCode222——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
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 ...
- [Swift]LeetCode222. 完全二叉树的节点个数 | Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
随机推荐
- javaSE之线程联合
首先定义 : 一个线程A在占有CPU资源期间 ,可以让其他线程调用join()和本线程联合. 嗯哈,像书本这个列子: 如: B.join(); 我们称A在运行期间联合了B, 如果线程A在占有CPU资源 ...
- HIbernate实现增、删、改、查。
//大配置 <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC " ...
- 详细解读Jquery各Ajax函数
$.get(),$.post(),$.ajax(),$.getJSON() 一,$.get(url,[data],[callback]) 说明:url为请求地址,data为请求数据的列表,callba ...
- Touch ID集成
作者感言 这个国庆由于种种原因, 过的不太安稳, 搬家, 办证, 东跑西跑, 忙的压根就不像是在过节....不过算了, 挑最后一天写写博文.最后:如果你有更好的建议或者对这篇文章有不满的地方, 请联系 ...
- Java 集合系列 02 Collection架构
java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...
- 319. Bulb Switcher——本质:迭代观察,然后找规律
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
- Maven 系列 一 :Maven 快速入门及简单使用【转】
开发环境 MyEclipse 2014 JDK 1.8 Maven 3.2.1 1.什么是Maven? Maven是一个项目管理工具,主要用于项目构建,依赖管理,项目信息管理. 2.下载及安装 下载最 ...
- 扫盲如何在ECLIPSE中使用条件断点
有时候在编码的时候我们希望知道代码变量符合某个条件时,才中断点,其他的情况不中断点. 解决办法1: 我们写个代码 判断,符合条件在符合条件处进行断点,这个方法很麻烦,需要去修改代码,不要是还需 ...
- shell学习记录003-cat命令
cat 命令一般用于文件的查看 cat -s file #可以去除文件中多余的上下空行 cat -T file #Python编程中会用到的制表符会在该命令中体现出来 cat -n file ...
- WCF服务编程中使用SvcMap实现类型共享等技巧【转】
原文链接:http://www.cr173.com/html/19026_1.html 国外的一篇文章:Sharing DataContracts between WCF Services 文中提到的 ...