leetcode_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.
思路1:
暴力解决,遍历树的所有节点,依次统计。
会超时,题目限定了该二叉树是完全二叉树,该种思路适用于所有二叉树。
思路2:
暴力解决无法测试通过,此时需要考虑完全二叉树的特点。
完全二叉树,至少有一课子树是完美二叉树,另一课子树至少是完全二叉树。
因此可以递归的计算。
判断完美二叉树:
最内侧的叶子结点和最外侧的叶子结点是否在同一层。
代码:
public static int countNodes(TreeNode root){
if(root == null){
return 0;
}
int hl = getLeft(root)+1;
int hr = getRight(root)+1;
if(hl == hr){
int num = (2<<(hl-1))-1;
return num;
}else{
return countNodes(root.left)+countNodes(root.right)+1;
}
}
public static int getRight(TreeNode root) {
int cou = 0;
TreeNode ptr = root;
while(ptr.right != null){
cou++;
ptr = ptr.right;
}
return cou;
}
public static int getLeft(TreeNode root) {
int cou = 0;
TreeNode ptr = root;
while(ptr.left != null){
cou++;
ptr = ptr.left;
}
return cou;
}
leetcode_222 Count Complete Tree Nodes的更多相关文章
- leetcode面试准备:Count Complete Tree Nodes
1 题目 Given a complete binary tree, count the number of nodes. In a complete binary tree every level, ...
- 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】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
原题链接在这里:https://leetcode.com/problems/count-complete-tree-nodes/ Given a complete binary tree, count ...
- [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- 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 ...
随机推荐
- jQuery取得select选中的值
$("#sxselect").change(function(){ alert($("#sxselect option:selected").val()); } ...
- linux recv 返回值与linux socket 错误分析
转载:http://blog.csdn.net/henry115/article/details/7054603 recv函数 int recv( SOCKET s, char FAR *buf, i ...
- Hive_初步见解,安装部署与测试
一.hive是什么东东 1. 个人理解 hive就是一个基于hdfs运行于MapReduce上的一个java项目, 这个项目封装了jdbc,根据hdfs编写了处理数据库的DDL/DML,自带的 二进制 ...
- Jquery,YUI这个著名js库名称作用的理解
看廖雪峰大神的教程,其中讲到变量作用域问题.在命名空间中,写到:因为全局变量绑到了window上,不同的js文件访问相同全局变量或者定义了相同名字的顶层函数,都会造成命名冲突,并且很难被发现. 减少冲 ...
- xss如何加载远程js的一些tips
在早期 , 对于xss我们是这样利用的 <script>window.open('http://xxx.xxx/cookie.asp?msg='+document.cookie)</ ...
- spring security防御会话伪造session攻击
1. 攻击场景 session fixation会话伪造攻击是一个蛮婉转的过程. 比如,当我要是使用session fixation攻击你的时候,首先访问这个网站,网站会创建一个会话,这时我可以把附有 ...
- 安装repcached,并且测试其双向复制是否成功
备注:本实验不仅包括了repcached,还包括了memcache的配置安装 1.1实验环境. 1.2环境准备. 1.3配置一个memcache. 1.3.1安装memcache. 1.3.2启动me ...
- IP地址的分类与寻址
IP地址:有一种标识符,被TCP/IP协议簇的IP层用来标识 连接到因特网的设备.IP协议的第4版IPv4地址是32位地址,是连接地址,定义了每一个连接到因特网上的设备(可以认为是主机的别名),而不是 ...
- c语言.大数的输出
转化成字符串,再用for循环输出: #include <stdio.h>#include <string.h>int main(){ char s[32]; int d, ...
- Suffix array
A suffix array is a sorted array of all suffixes of a given string. The definition is similar to Suf ...