LeetCode OJ 222. Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes.
Definition of a complete binary tree from Wikipedia:
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.
如果用暴力递归来计算节点总数,那么会超时,时间复杂度是O(N)。由于我们求的是完全二叉树的节点数,如果用求普通二叉树的方法来解决肯定是不合适的。完全二叉树的一些特点可能成为我们解决该问题的思路:如果一个节点的左子树的深度和右子树的深度一样,那么以该节点为根节点的树的节点数为:h2-1。最好情况下的时间复杂度为O(h),其它情况的时间复杂度为O(h2)。代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int countNodes(TreeNode root) {
if(root==null) return 0; int l = getLeft(root) + 1;
int r = getRight(root) + 1; if(l==r) {
return (2<<(l-1)) - 1;
} else {
return countNodes(root.left) + countNodes(root.right) + 1;
}
} private int getLeft(TreeNode root) {
int count = 0;
while(root.left!=null) {
root = root.left;
++count;
}
return count;
} private int getRight(TreeNode root) {
int count = 0;
while(root.right!=null) {
root = root.right;
++count;
}
return count;
}
}
LeetCode OJ 222. 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】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 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
- leetcode面试准备:Count Complete Tree Nodes
1 题目 Given a complete binary tree, count the number of nodes. In a complete binary tree every level, ...
- [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- Java for LeetCode 222 Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
随机推荐
- (转载)uefi启动解析:由原理到实例
这是本文的全部内容:A:什么是UEFI,其含义B:UEFI模块包含的文件逐个分析及其引导流程+ESP分区的本质C:判断自己的机器是X64(64 bit)构架还是IA32(32 bit)的构架D:给传统 ...
- java.net.ConnectException connect refured
原因:tomcat 连接拒绝:tomcat没有完全重启 只是部分重启 解决方案: 连接tomcat服务 命令:1:ps -ef|grep java : 2:kill -9 21060 3:查看tomc ...
- AngularJs应用
引用angularjs文件 AngularJS 应用组成如下:View(视图), 即 HTML.Model(模型), 当前视图中可用的数据.Controller(控制器), 即 JavaScript ...
- .NET架构师
闲话不多扯,关于.NET架构师的培训 架构师的知识体系总结:7大重点,对7大重点作为细节的阐述将再后面陆续展开!架构师的体系纲领主要来着这7点.(必须严格记下) 1:现代软件开发过程及架构策略 1. ...
- iosTableView 局部全部刷新以及删除编辑操作
局部刷新方法 添加数据 NSArray *indexPaths = @[ [NSIndexPath indexPathForRow:0 inSection:0], [NSIndexPath index ...
- 第六十六节,htnl音频视频
htnl音频视频 学习要点: 1.音频和视频概述 2.video视频元素 3.audio音频元素 本章主要探讨HTML5中音频和视频元素,通过这两个原生的媒体元素向HTML页面 ...
- IOS 类似网易新闻客户端内容滚动菜单跟随居中组件
需求分析: 1.类似网易新闻客户端页面滚动组件.菜单栏对应菜单项一直居中 2.点击菜单栏可以切换到对应的page 3.滑动页面可以自动切换相应的菜单.并且对应的菜单栏居中显示 4.初始化时可以自定义菜 ...
- clion idea jetbrain windows下搞c/c++
安装 clion 把MinGW(搜MinGW.zip)放到c盘根目录下 ok
- javascript焦点图自动缓冲滚动
html中调用的js库,之前的随笔中有写,就不细说了,不明白的可以留言给我 <!DOCTYPE html> <html> <head> <meta chars ...
- 解决没有X11/Xlib.h 的错误
1. 解决没有X11/Xlib.h 的错误Install XLib $sudo apt-get install libx11-dev 2. 提示错误:未找到软件源 解决办法很简单,更换另一个源就行了. ...