题目链接:https://leetcode.com/problems/count-complete-tree-nodes/#/description

题目要求:计算完全二叉树的节点个数

思路:首先想到的是直接的递归,二叉树的节点个数 = 左子树的节点个数 + 右子树的节点个数 + 1

Java 代码如下:

public class Solution {// 二叉树的节点数 = 左子树的节点数 + 右子树的节点数 + 1public int countNodes(TreeNode root) {if(root == null){return 0;}return countNodes(root.left) + countNodes(root.right) + 1;  }
}

遍历了整个二叉树,时间复杂度为O(N)。然后超时了,因为上述方法并没有使用“完全二叉树”这个条件。

接下来考虑,对于完全二叉树,其左子树和右子树中至少有一个子树是满二叉树,而满二叉树的节点个数可以直接由 2^n-1得到,因此,是满二叉树的那一部分就不需要再遍历,因此可以提高效率。算法思路如下:首先计算出二叉树的最左侧分支和最右侧分支的层数,如果二者相等,则整个二叉树是满二叉树;若不相等,则递归的计算左右子树的节点数,总结点数=左子树节点数+右子树节点数+1。

Java代码如下:

public class Solution {// 获取左子树的高度(其实是最左侧分支)public int getLeftHeight(TreeNode root) {int count = 0;while (root != null) {count++;root = root.left;}return count;}// 获取右子树的高度(其实是最右侧分支的高度)public int getRightHeight(TreeNode root) {int count = 0;while (root != null) {count++;root = root.right;}return count;}public int countNodes(TreeNode root) {if (root == null) {return 0;}int leftHeight = getLeftHeight(root);int rightHeight = getRightHeight(root);if (leftHeight == rightHeight) {// 表示是满二叉树,二叉树的节点数直接由公式2^n-1得到// leftHeight即为层数, 1 << leftHeight使用位运算计算2^leftHeight,效率更高// 注意(1 << leftHeight) - 1 的括号必须有!!return (1 << leftHeight) - 1;} else {// 若该二叉树不是满二叉树,递归的调用该方法,计算左子树和右子树的节点数return countNodes(root.left) + countNodes(root.right) + 1;}}
}

LeetCode 222. Count Complete Tree Nodes 题解——Java相关推荐

  1. leetcode 222. Count Complete Tree Nodes | 222. 完全二叉树的节点个数(Java)

    题目 https://leetcode.com/problems/count-complete-tree-nodes/ 题解 思路参考左程云<程序员代码面试指南> 顺便贴一下草稿 代码 c ...

  2. leetcode 222 Count Complete Tree Nodes

    今天做了一道leetcode题,刚开始是暴力破解,发现special judge, very large tree 过不去,然后一顿想,一顿查,发现可以运用complete binary tree 的 ...

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

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

  4. Middle-题目95:222. Count Complete Tree Nodes

    题目原文: Given a complete binary tree, count the number of nodes. 题目大意: 给出一个完全二叉树,求节点数. 题目分析: 如果直接递归数节点 ...

  5. 222. Count Complete Tree Nodes

    题目: Given a complete binary tree, count the number of nodes. Definition of a complete binary tree fr ...

  6. 222 Count Complete Tree Nodes

    1,这道题如果纯用递归数点而不利用其为一个complete binary tree的话会超时. 2.为了利用这个条件,比较左右两子数的高度:1, 如果相等则左子树为完全二叉树 2, 如果不等, 则右子 ...

  7. LeetCode Count Complete Tree Nodes(二分法)

    问题:给出一个完全二叉树,求其结点个数 思路:第一种方式时直接使用递归法,将其左子树个数加上右子树个数再加上根结点 第二种方式二分法,因为完全二叉树除了最后一层外,其它都满足有2^i个结点,而最后一层 ...

  8. Count Complete Tree Nodes

    https://leetcode.com/problems/count-complete-tree-nodes/ 宽度优先搜索方法,超时!! /*** Definition for a binary ...

  9. LeetCode题解-222-Count Complete Tree Nodes

    没有做出来,所以参考了https://segmentfault.com/a/1190000003818177 迭代法的图解如下:

最新文章

  1. 探测参考信号(Sounding Reference Signal)
  2. 操作所有的数据库免费可视化界面靠它就够了,告别付费,告别白嫖,卸载Navicat!
  3. HDU 6035 Colorful Tree(补集思想+树形DP)
  4. python对excel操作简书_Python读写Excel表格,就是这么简单粗暴又好用
  5. 基于KVM的虚拟化研究及应用
  6. 数据中台VS业务中台、数据中台VS数据仓库,到底有什么区别?
  7. 风吹雨云商城系统程序源码
  8. python 面试题(1)--- python模块
  9. 一次性搞清Java中的类加载问题
  10. CWnd的派生类-3、CDialog类
  11. linux下pwd命令学习
  12. ARP过程——删除地址映射
  13. jxl读数据库数据生成xls 并下载
  14. python 字典 列表 深度遍历_如何完全遍历未知深度的复杂字典?
  15. 《和平精英》:新军需山经魅狐、滑板小狐今日正式上线,很帅气!
  16. 2017年软考程序员考试填涂答题卡(纸)注意事项
  17. 人人都该懂点儿TCP
  18. IDEA+EmmyLua配置
  19. ubuntu系统给u盘拷贝文件
  20. 使用GoldWave制作“淡入/淡出”效果

热门文章

  1. pro_cs6 经验
  2. 智能电视看凤凰卫视,不用直播源
  3. Amos--方差估计与假设检验
  4. 接口测试平台-112: 首页优化2期 竖线右侧:数据看板+图形看板
  5. 腾讯公司副总裁王巨宏:腾讯互联网+教育做好连接和内容两件事
  6. 微信分享接口SDK(前端js和后端php)
  7. 手把手教你设置 IntelliJ IDEA 的彩色代码主题
  8. nCode:DesignLife案例教程十一
  9. 物联卡是什么?有什么用?可以用在什么设备上?
  10. Vue全家桶构建项目