【Leetcode】222. Count Complete Tree Nodes
Question:
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.
Tips:
给定一个完全二叉树,求树中结点的个数。
思路:
完全二叉树的特性:完全二叉树:叶节点只能出现在最下层和次下层,并且最下面一层的结点都集中在该层最左边的若干位置的二叉树。即除了最后一层,前面所有层必须是满二叉树。这样我们就只要找到最后一层几个叶子节点就可以判断树中结点的数量。
只有当左右子树高度想同的时候,才能知道该节点之前的树是满的。
当左右子树高度不相等,采用递归的办法,来判断其做左右子树的高度是否相等。
代码:
//根据完全二叉树的特性 除最后一排 前面的树是满二叉树。
public int countNodes(TreeNode root) {
if (root == null)
return 0;
int ans=0;
int left=getLeft(root);
int right=getRight(root);
if(left==right){
return (1<<left)-1;
}else
return 1+countNodes(root.left)+countNodes(root.right);
} private int getLeft(TreeNode root) {
int h=0;
while(root!=null){
h++;
root=root.left;
}
return h;
}
private int getRight(TreeNode root) {
int h=0;
while(root!=null){
h++;
root=root.right;
}
return h;
}
【Leetcode】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(完全二叉树)
Given the root of a complete binary tree, return the number of the nodes in the tree. According to W ...
- 【刷题-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 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 ...
- 【刷题笔记】LeetCode 222. Count Complete Tree Nodes
题意 给一棵 complete binary tree,数数看一共有多少个结点.做题链接 直观做法:递归 var countNodes = function(root) { if(root===nul ...
- 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 ...
随机推荐
- luogu P1891 疯狂LCM
嘟嘟嘟 这题跟上一道题有点像,但是我还是没推出来--菜啊 \[\begin{align*} ans &= \sum_{i = 1} ^ {n} \frac{i * n}{gcd(i, n)} ...
- FastJson遇见的问题或项目实战中优化的问题,看源码都可以解决
1:感觉见鬼了一般存储JSONObject中的字段竟然不见了? JSONObject object=new JSONObject(); Map fields = new HashMap(); fiel ...
- kubernetes备份和恢复
kubernetes备份和恢复 备份etcd数据 首先由于ETCD有三个备份,并且会同步,所以您只需要在一台master机器上执行ETCD备份即可. 另外在运行下列命令前,确保当前机器的kube- ...
- python学习课件
张鑫 18511446896 ------------------------------------------------------------------------------------- ...
- IDEA导包(以junit为例)
## IDEA导包(以junit为例) 1. 准备junit的jar包: * hamcrest-core-1.3.jar * junit-4.12.jar 2. 在项目中新建文件夹:lib 3. 将j ...
- CUDA2.4-原理之性能优化及浮点运算
本部分来自于<大规模并行处理器编程实战>第六章.第七章.打算不再看这本书了,准备看<programming massively parallel processors 2nd> ...
- 【js】某字符串多次替换
eg: var str="icon icon-1chkedchkedchked"; var strNew=str.replace(new RegExp("chked&q ...
- centos 64位系统安装
由于centos 64位镜像大于4G,所以U盘装不进去.iso镜像,选择网络安装的方法或者使用一个U盘制作启动盘和一个硬盘来装镜像的方法. 1 网络安装第一步 下载 CentOS 安装 ISO 浏览 ...
- SEO优化上首页之搜索引擎排名规则
搜索引擎建立索引的网页数以万亿计,用户搜索的关键词也是海量,如果每个用户提交搜索请求后,搜索引擎都去数以万亿的索引中重新排名网页,效率将非常低下.根据2-8法则,80%是查询是集中在相同的20%内容上 ...
- HTML-JS 循环 函数 递归
[循环结构的执行步骤] 1.声明循环变量 2.判断循环条件 3.执行循环体操作 4.更新循环变量 然后,循环执行2-4,直到条件不成立时,跳出循环. while循环()中的表达式,运算结果可以是各种类 ...