题目:

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. ORA-01034错误:ORALCE NOT CONNECT

    解决办法: 启动数据库 sqlplus '/as sysdba '  startup;

  2. BZOJ3625: [Codeforces Round #250]小朋友和二叉树

    Description 我们的小朋友很喜欢计算机科学,而且尤其喜欢二叉树.考虑一个含有n个互异正整数的序列c[1],c[2],...,c[n].如果一棵带点权的有根二叉树满足其所有顶点的权值都在集合{ ...

  3. HDU 1520 树形dp裸题

    1.HDU 1520  Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> ...

  4. Java_动态加载

    Java类动态加载(一)——java源文件动态编译为class文件最近在做java动态加载这方面的工作,起初也遇到了很多困难.网上关于这方便的东西很零散,为了便于日后回过头来再看,于是我将这几天的心得 ...

  5. Hibernate条件查询

    设计上可以灵活的根据 Criteria 的特点来方便地进行查询条件的组装.现在对 Hibernate的Criteria 的用法进行总结:Hibernate 设计了 CriteriaSpecificat ...

  6. Android studio 一个项目中添加两个module遇到的bug

    1.在一个Android studio中,我添加了一个模块,然后就是各种bug 找到到R 是在module 名上面 右键 Make Module '模块名' 经过各种google 的时候发现了 htt ...

  7. OSG 自定义数据类型 关键帧动画

    OSG 自定义数据类型 关键帧动画 转自:http://blog.csdn.net/zhuyingqingfen/article/details/12651017 /* 1.创建一个AnimManag ...

  8. Oracle连接超时

    客户机 要想 连接 oracle服务机(装了数据库的机子),只需要在客户机配置一个 本地服务名就行了,不管你用Oracle服务端的配置工具 还是客户端的配置工具,都没有问题. 如果只是客户机连接服务机 ...

  9. 模拟ATM机银行系统

    淄博汉企Java基础考核项目 模拟银行自助终端系统 一. 本系统模拟银行用户使用ATM机开户.查询.存款.取款功能,要求使用java语言编程实现. 说明: 1. 对于数据输入异常,可使用java异常处 ...

  10. Odoo 仓库扫码打包方案

        Odoo仓库扫码的设计,前提是操作人在PC上先做好分拣单,然后根据打印出来的分拣单去仓库进行扫码打包,默认的情况下,分拣在被确认的时候会自动保留库位中已经存在的库存(已经分配批次\序列号),而 ...