leetcode面试准备:Count Complete Tree Nodes
1 题目
Given a complete binary tree, count the number of nodes.
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.
接口:public int countNodes(TreeNode root);
2 思路
思路1
用暴力法:不考虑完全二叉树的特性,直接遍历求节点数。---超时
复杂度: Time:O(N); Space:O(log N)
思路2
完全二叉树的节点数,可以用公式算出 2^h - 1. 如果高度不相等, 则递归调用 countNodes(root.left) + 1 + countNodes(root.right)
复杂度: Time:O(log N); Space:O(log N)
思路3
二分查找的思想,感觉和思路2差不多。但是代码写出来的效率高一些。
复杂度: Time:O(log N); Space:O(1)
3 代码
思路1
public int countNodes0(TreeNode root) {
if (root == null)
return 0;
return countNodes(root.left) + 1 + countNodes(root.right);
}
思路2
public int countNodes(TreeNode root) {
if (root == null)
return 0;
int leftHigh = 0, rightHigh = 0;
TreeNode lchild = root.left, rchild = root.right;
for (; lchild != null;) {
leftHigh++;
lchild = lchild.left;
}
for (; rchild != null;) {
rightHigh++;
rchild = rchild.right;
}
if (leftHigh == rightHigh) {
return (2 << leftHigh) - 1;
} else {
return countNodes(root.left) + 1 + countNodes(root.right);
}
}
4 总结
计算阶乘的注意: (2 << leftHigh) - 1才不会超时,用Math.pow()超时。
二分查找思想,自己很不熟练。多找题目联系联系。
5 参考
leetcode面试准备:Count Complete Tree Nodes的更多相关文章
- 【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 OJ 222. Count Complete Tree Nodes
Total Accepted: 32628 Total Submissions: 129569 Difficulty: Medium Given a complete binary tree, cou ...
- 【Leetcode】222. Count Complete Tree Nodes
Question: Given a complete binary tree, count the number of nodes. Definition of a complete binary t ...
- LeetCode OJ: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 the root of a complete binary tree, return the number of the nodes in the tree. According to W ...
- 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 Count Complete Tree Nodes
原题链接在这里:https://leetcode.com/problems/count-complete-tree-nodes/ Given a complete binary tree, count ...
随机推荐
- android 中 ColorDrawable dw = new ColorDrawable(0x3ccccccc),关于颜色定义的总结
android 中 ColorDrawable dw = new ColorDrawable(0x3ccccccc),关于颜色定义的总结 0x3ccccccc 拆分开来 0x-3c-cccccc ...
- IT技能栈
C++.JAVA.Objective-C 基本数据类型,集合类如字符串数组字典,自定义数据对象 内存布局,编译运行期的变化 语言特性 输入输出流,文件流,序列化 多线程,并发控制,线程池,锁 网络编程 ...
- SELECT [Code] ,[AlarmID] ,[ItemName] ,[isDeleted] ,[Remark] FROM [LjlData].[dbo].[T_BaseDetail] union select 0--
SELECT [id] ,[AlarmID] ,[ItemName] ,[isDeleted] ,[Remark] FROM [LjlData]. ...
- Base64的Java代码实现
欢迎拍砖~ 在数据二进制和byte互相转换的地方方法写得有点挫,不知道有没有更好的方法~ 顺便复习了java的一些基础东西,如位操作,原码反码补码 可以在这篇blog里学习到详细的知识点:http:/ ...
- [XML] C#ResourceManagerWrapper帮助类 (转载)
点击下载 ResourceManagerWrapper.rar /// <summary> /// 类说明:ResourceManagerWrapper /// 编 码 人:苏飞 /// ...
- Tomcat - java.lang.UnsupportedClassVersionError:Unsupported major.minor version 51.0 (unable to load class com.microsoft.sqlserver.jdbc.SQLS
今天使用Tomcat连接sql Server 2008 enterprise的时候,报错: HTTP Status 500 - Servlet execution threw an exception ...
- 深入理解shared pool共享池之library cache的library cache lock系列四
本文了解下等待事件library cache lock,进一步理解library cache,之前的文章请见: 深入理解shared pool共享池之library cache的library ca ...
- 如何改app图标名称
InfoPlist.strings文件里写上: CFBundleDisplayName="中文名字";
- WinpCap 使用线程发数,明明发了,返回值0是OK的啊,怎么抓包看不到,难道不支持多线程。。。
if (!m_adapterHandle){ return false;}int rst = pcap_sendpacket((pcap_t*)m_adapterHandle,data ,dat ...
- linux 的一个防火墙策略
#警告:在运行脚本后,勿必单独运行 iptables -F#因为脚本包含的默认规则为“禁止所有访问”#当其它规则被清除后,系统表现为无法访问状态,需要重启系统恢复#iptables -L 查看当前已应 ...