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 ...
随机推荐
- 基于TCP协议的网络编程
TCP通信协议是一种可靠的传输层协议,它在通信的两端各建立一个Socket,从而在通信的两端之间形成虚拟网络链路.一旦建立了虚拟的网络链路,两端的程序就可以通过虚拟链路进行通信.Java使用Socke ...
- 《JavaScript高级程序设计》读书笔记 ---单体内置对象
Global对象Global(全局)对象可以说是ECMAScript 中最特别的一个对象了,因为不管你从什么角度上看,这个对象都是不存在的.ECMAScript 中的Global 对象在某种意义上是作 ...
- 【MySQL】使用 Optimizer Trace 观察SQL执行过程
Optimizer Trace 是MySQL 5.6.3里新加的一个特性,可以把MySQL Optimizer的决策和执行过程输出成文本.输出使用JSON格式,便于程序分析和人类阅读. 使用方法 1) ...
- java中的词汇
java中的词汇: 空白符:空格,制表符,换行符.他们的存在使得代码变得很美观. 标识符:由大小写字母,数字,下划线,美元符号组成.且数字不能用于标识符第一个字符. 字面值:变量的值通常使用表示常量的 ...
- 菜鸟进阶——grunt
为保证作者版权在此声明本文部分摘自 http://yujiangshui.com/grunt-basic-tutorial/ 另,参考文章 http://www.tuicool.com/artic ...
- 在js中拼接<a>标签,<a>标签中含有onclick事件,点击无法触发该事件
我们在<a>标签中添加事件一般是onclick="editUser()" 这样添加,在html页面上是行的通的 但是如何你是在js中拼接<a>标签并在< ...
- JS逻辑运算大于小于比较
遇到这个问题,结果测试了好半天终于发现原因, 例子: var az = $('#a').css('zIndex'); // 1001 var bz = $('#b').css('zIndex'); / ...
- Office在线预览及PDF在线预览的实现方式大集合
一.服务器先转换为PDF,再转换为SWF,最后通过网页加载Flash预览 微软方:利用Office2007以上版本的一个PDF插件SaveAsPDFandXPS.exe可以导出PDF文件,然后再利用免 ...
- 简单的XML和JSON数据的处理
一.XML格式装换成json数据格式 using System; using System.Collections.Generic; using System.Linq; using System.W ...
- html5权威指南:表格元素
第十一章:表格元素 ...