题目:

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的更多相关文章

  1. leetcode面试准备:Count Complete Tree Nodes

    1 题目 Given a complete binary tree, count the number of nodes. In a complete binary tree every level, ...

  2. leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes

    完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...

  3. 完全二叉树的节点个数 Count Complete Tree Nodes

    2018-09-25 16:36:25 问题描述: 问题求解: 单纯遍历了一遍,emmm,果然TLE. 解题思路就是比较左边树高度和右边树高度,如果相等,那么就是一个满二叉树,返回1 << ...

  4. 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)

    [LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...

  5. 【刷题-LeetCode】222. Count Complete Tree Nodes

    Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. Note: Definition ...

  6. LeetCode Count Complete Tree Nodes

    原题链接在这里:https://leetcode.com/problems/count-complete-tree-nodes/ Given a complete binary tree, count ...

  7. [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  8. Count Complete Tree Nodes

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  9. 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 ...

随机推荐

  1. 【BZOJ】3712: [PA2014]Fiolki

    http://www.lydsy.com/JudgeOnline/problem.php?id=3712 题意:n个瓶子,第i个瓶子里又g[i]克物质.m次操作,第i次操作把第a[i]个瓶子的东西全部 ...

  2. Eclipse IDE for C/C++ Developers安装配置详解

    Eclipse IDE for C/C++ Developers安装配置详解(转) 转自:http://hi.baidu.com/ltb6w/item/986532efd712460f570f1ddc ...

  3. java实现BitMap

    package bitmap; public class BitMap { private byte[] bytes; public BitMap(byte[] bytes) { super(); t ...

  4. orcale 动态执行语句

    create or replace function fn_test2(tablename in varchar2) return number is rtn number; begin --通用的获 ...

  5. Js练笔——用循环和递归实现追踪对象深度(循环引用关系不考虑)

    function reobs(obj){ //返回对象中对象属性组成的数组 var a=[]; var b=[]; for(it in obj){ a.push(it); } for(var i=0; ...

  6. html图片和文字的细节

    ul中的每一个li如果里面添加“一个图,一行字”, 这样图片会紧贴在左侧,而文字会居中,这两个元素不会紧贴着. 产生这种问题的原因我推测是:我图片设置了左浮动,但文字没有设置浮动,而一旦将文字设置为浮 ...

  7. AOP 学习

    学习 Spring.Net 的AOP 的时候,在做一个简单的测试例子的时候,配置文件和代码逻辑都是没问题的,但始终报这样一个异常: 无法将类型为“CompositionAopProxy_1e76f37 ...

  8. CloudSim样例分析

    自带八个样例描述: cloudsim-2.1.1\examples目录下提供了一些CloudSim样例程序,每个样例模拟的环境如下: (1)CloudSimExample1.Java:创建一个一台主机 ...

  9. SpringMvc简单实例

    Spring MVC应用一般包括4个步骤: (1)配置web.xml,指定业务层对应的spring配置文件,定义DispatcherServlet; (2)编写处理请求的控制器 (3)编写视图对象,例 ...

  10. psutil 是因为该包能提升 memory_profiler 的性能

    python 性能分析入门指南 一点号数据玩家昨天 限时干货下载:添加微信公众号"数据玩家「fbigdata」" 回复[7]免费获取[完整数据分析资料!(包括SPSS.SAS.SQ ...