完全二叉树的节点个数 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 ...
随机推荐
- windows 10下oracle相关异常及处理方法
话说起来,不以oracle性能优化,数据库维护为主业已经有四五年了,这两年基本上以mysql为主. pl/sql登录后提示空白对话框.将ORACLE_HOME设置为oracle 11g的目录. IMP ...
- 【4OpenCV】OpenCV和RTSP的综合研究
一.RTSP是什么?用来干什么? RTSP(Real Time Streaming Protocol),RFC2326,实时流传输协议,是TCP/IP协议体系中的一个应用层协议,由哥伦比亚大学.网景和 ...
- Java基础语法(上)
Java编译报错出现非法字符,原因是存在中文字符. Java关键字的字母都是小写. Java是一种强类型语言,针对每一种数据都给出了明确的数据类型. 数据类型分类: A:基本数据类型 B:引用数据类型 ...
- wait()和notify()的理解与使用
void notify() Wakes up a single thread that is waiting on this object’s monitor. 译:唤醒在此对象监视器上等待的单个线程 ...
- Access导出excel
SELECT * INTO [excel .xls].Sheet1 FROM tableName
- DataSnap下的分包获取
DataSnap下通过TQuery—TDataSetProvider—TClientDataSet获取数据,如果是主从数据,则每条主表记录都会触发从表数据的获取. 这种获取和组织数据的方式有一个问题: ...
- Android 充电信息的获取【转】
本文转载自:https://blog.csdn.net/wateryi/article/details/50834821 在android系统中,电池信息是由BatteryService.java统一 ...
- poj2774
思路 求出height之后 只要相邻两个子串是本串不同的来更新就好 因为这样一定是最优啊..取min显然越长越不好 (这里'%'当成'{'吧) abc%bca height i sa belong 0 ...
- LuoguP2680 运输计划
题目地址 题目链接 题解 二分答案,那么大于答案的路径都需要有一条公共边,maxlen-val>=二分出来的x.val是边权. 考虑树剖,对每条大于答案的路径都+1(线段树里),枚举边,如果(线 ...
- (转)Introductory guide to Generative Adversarial Networks (GANs) and their promise!
Introductory guide to Generative Adversarial Networks (GANs) and their promise! Introduction Neural ...