完全二叉树的节点个数 Count Complete Tree Nodes
2018-09-25 16:36:25
问题描述:

问题求解:
单纯遍历了一遍,emmm,果然TLE。
解题思路就是比较左边树高度和右边树高度,如果相等,那么就是一个满二叉树,返回1 << h - 1即可,如果不是,则递归的计算左右子树的个数。
时间复杂度:O(logn * logn)
public int countNodes(TreeNode root) {
if (root == null) return 0;
int l = leftHeight(root);
int r = rightHeight(root);
if (l == r) return (1 << l) - 1;
else return 1 + countNodes(root.left) + countNodes(root.right);
}
private int leftHeight(TreeNode root) {
if (root == null) return 0;
return 1 + leftHeight(root.left);
}
private int rightHeight(TreeNode root) {
if (root == null) return 0;
return 1 + rightHeight(root.right);
}
2019-04-15 14:42:44
其实我根本不用管二叉树的种类,直接递归去计数就可以了,在log(n)的时间复杂度内完成,且速度完胜之前的解法。
Runtime: 0 ms, faster than 100.00% of Java online submissions for Count Complete Tree Nodes.
public int countNodes(TreeNode root) {
if (root == null) return 0;
return countNodes(root.left) + countNodes(root.right) + 1;
}
完全二叉树的节点个数 Count Complete Tree Nodes的更多相关文章
- [Swift]LeetCode222. 完全二叉树的节点个数 | Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- 【LeetCode 222_完全二叉树_遍历】Count Complete Tree Nodes
解法一:递归 int countNodes(TreeNode* root) { if (root == NULL) ; TreeNode *pLeft = root->left; TreeNod ...
- leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
- 【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
1 题目 Given a complete binary tree, count the number of nodes. In a complete binary tree every level, ...
- [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 ...
- 222. Count Complete Tree Nodes -- 求完全二叉树节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
随机推荐
- 【题解】Luogu P4979 矿洞:坍塌
原题传送门:P4979 矿洞:坍塌 这是某场膜你赛的题,最后我一百零几分rank三十几滚粗 这是我唯一ac的一题 这题比较简单qaq 前置芝士:珂朵莉树 窝博客里对珂朵莉树的介绍 没什么好说的自己看看 ...
- 通过注册表regedit对Windows回收站进行恢复
误删资料恢复 一不小心,删错了,还把回收站清空了,咋办啊? 只要三步,你就能找回你删掉并清空回收站的东西 步骤: 1.单击"开始——运行,然后输入regedit(打开注册表) 2.依次展开: ...
- 02:saltstack-api使用详解
1.1 salt-api安装 参考博客:https://www.jianshu.com/p/012ccdff93cc 1.介绍 1. saltsatck本身就提供了一套算完整的api,使用 Che ...
- 复制MIFARE Classic卡
Mifare Classic 1K智能卡介绍及nfc-tools的使用 [原创]RFID安全之——ACR122U菜鸟初体验-『智能设备』-看雪安全论坛 复制MIFARE Classic小区门禁卡记录 ...
- centos7 update network time
yum install -y ntp crontab -e */5 * * * * /usr/bin/ntpdate ntp.api.bz ### ntp.api.bz 是一组NTP集群服务器,之 ...
- [内核驱动] 链表LIST_ENTRY的操作(转)
转载:https://www.cnblogs.com/forlina/archive/2011/08/11/2134610.html 转载:http://www.xuebuyuan.com/15443 ...
- QWidget设置背景图
1.使用QSS出现很多问题 2.方法 this->setAutoFillBackground(true); QPalette palette = this->palette(); pale ...
- topcoder srm 400 div1
problem1 link 枚举指数,然后判断是不是素数即可. problem2 link 令$f[len][a][b][r]$(r=0或者1)表示子串$init[a,a+len-1]$匹配$goal ...
- topcoder srm 713 div1
problem1 link 如果$a^{b}=c^{d}$,那么一定存在$t,x,y$使得$a=t^{x},c=t^{y}$.一旦$t,x,y$确定,那么可以直接计算出二元组$b,d$有多少.对于$t ...
- SSM项目开发中的实体定义以及MySQL表格设计
话不多说,下面表格是项目开发中用到的实体集以及表格Name 实体创建 表格创建 Area 区域 Area 实体 areaId areaName priority createTime last ...