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 ...
随机推荐
- Objective-C 内存管理与高级环境编程 阅读分享
常用的调试私有API uintptr_t objc_rootRetainCount(id obj) _objc_autoreleasePoolPrint();//查看自动释放池中的对象 LLVM cl ...
- find grep使用
-------------------------------------find---grep---------------------------------------- 在当前目录下所有文件中 ...
- virtualbox 安装windows系统的一些问题
今天总结一下,使用virtualbox安装windows系统的一些问题. 安装的是Ghost的系统,正版系统也可以参考. 首先本人的机器原系统是ubuntu 16.04 LTS x64 1.win7或 ...
- return与finally
当return遇到了finally,先标记return的值,然后执行finally,当finally修改了return的值,那么执行finally后,传递最后一次return的值,若finally没有 ...
- webrtc学习———记录三:mediaStreamTrack
参考: https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack 转自http://c.tieba.baidu.com/p/3 ...
- RelativeLayout相对布局 安卓布局技巧
http://blog.csdn.net/nieweiking/article/details/38417317 RelativeLayout相对布局 相对布局 RelativeLayout 允许子元 ...
- json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
注:在项目中, 我使用原生的ajax请求数据的时候, JSONObject没能帮我解析, 当却不给我报错, 我是在junit单元测试中测试的时候, 发现的.发现好多时候, 特别是通过ajax请求, 不 ...
- IOS-UIScrollView实现图片分页
1.设置可以分页 _scrollView.pagingEnabled = YES; 2.添加PageControl UIPageControl *pageControl = [[UIPageContr ...
- Python之练习Demo
遍历本地文件系统 (sys, os, path),例如写一个程序统计一个目录下所有文件大小并按各种条件排序并保存结果,代码如下: #coding:GBK import os; def SortList ...
- __unset()魔术方法 删除类内私有属性
__unset()魔术方法 删除私有属性 unset()对共有属性进行删除 可通过__unset()魔术方法对私有属性进行操作 当在类外部执行unset()函数时,自动执行类内__unset()魔术方 ...