【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 ...
随机推荐
- C++的技术探究
C++深究 函数指针 double pam(int, double); // prototype double (*pf)(int, double); // declare function poin ...
- 6、JUC--同步锁Lock
显示锁 Lock 在Java 5.0之前,协调共享对象的访问时可以使用的机 制只有 synchronized 和 volatile .Java 5.0 后增加了一些 新的机制,但并不是一种替代内置 ...
- layui小封装方法
//打开加载动画function LayerLoad() { layui.use('layer', function () { var layer = layui.layer; layer.load( ...
- vmware共享文件夹
环境: VMware Workstation 11.0 虚拟机中的系统:Ubuntu 16.04 物理机:window 7 安装好vmware tools后在 /mnt/hgfs 里没有东西,是空白的 ...
- [Lydsy1805月赛]口算训练 BZOJ5358
分析: 没想到这道题还能二分查找... 这题主席树的话,裸的很显然...我们将每一个数分解质因数,之后建一个可持久化权值线段树维护[L,R]区间内的每一种质因子的个数,分解质因数的话,可以选择用线筛, ...
- linux_vim
今天稍微学习以下vim 学习的一些课件: 1. 2. 3. 4. 5.
- 使用Fortify进行代码静态分析(系列文章)
BUG级别:低 Code Correctness(代码正确性) 1.Class does not Implement Equals(类未能实现Equals方法) Dead Code(死亡代码) 1.U ...
- [LOJ#2878]. 「JOISC 2014 Day2」邮戳拉力赛[括号序列dp]
题意 题目链接 分析 如果走到了下行车站就一定会在前面的某个车站走回上行车站,可以看成是一对括号. 我们要求的就是 类似 代价最小的括号序列匹配问题,定义 f(i,j) 表示到 i 有 j 个左括号没 ...
- Spring+SpringMVC+MyBatis整合基础篇(三)搭建步骤
作者:13GitHub:https://github.com/ZHENFENG13版权声明:本文为原创文章,未经允许不得转载. 框架介绍 Spring SpringMVC MyBatis easyUI ...
- 移动端jq及zepto事件绑定
最近做移动端网页,用到了zepto.js , 其大致用法跟 jquery 差不多,但是在时间绑定的时候被困了好久的坑. 这里说的主要是给未来元素绑定事件.未来元素:这里指的是通过 ajax 请求得到数 ...