1 题目

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

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.

接口:public int countNodes(TreeNode root);

2 思路

思路1

用暴力法:不考虑完全二叉树的特性,直接遍历求节点数。---超时

复杂度: Time:O(N); Space:O(log N)

思路2

完全二叉树的节点数,可以用公式算出 2^h - 1. 如果高度不相等, 则递归调用 countNodes(root.left) + 1 + countNodes(root.right)

复杂度: Time:O(log N); Space:O(log N)

思路3

二分查找的思想,感觉和思路2差不多。但是代码写出来的效率高一些。

复杂度: Time:O(log N); Space:O(1)

3 代码

思路1

  1. public int countNodes0(TreeNode root) {
  2. if (root == null)
  3. return 0;
  4. return countNodes(root.left) + 1 + countNodes(root.right);
  5. }

思路2

  1. public int countNodes(TreeNode root) {
  2. if (root == null)
  3. return 0;
  4. int leftHigh = 0, rightHigh = 0;
  5. TreeNode lchild = root.left, rchild = root.right;
  6. for (; lchild != null;) {
  7. leftHigh++;
  8. lchild = lchild.left;
  9. }
  10. for (; rchild != null;) {
  11. rightHigh++;
  12. rchild = rchild.right;
  13. }
  14. if (leftHigh == rightHigh) {
  15. return (2 << leftHigh) - 1;
  16. } else {
  17. return countNodes(root.left) + 1 + countNodes(root.right);
  18. }
  19. }

4 总结

计算阶乘的注意: (2 << leftHigh) - 1才不会超时,用Math.pow()超时。

二分查找思想,自己很不熟练。多找题目联系联系。

5 参考

leetcode面试准备:Count Complete Tree Nodes的更多相关文章

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

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

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

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

  3. LeetCode OJ 222. Count Complete Tree Nodes

    Total Accepted: 32628 Total Submissions: 129569 Difficulty: Medium Given a complete binary tree, cou ...

  4. 【Leetcode】222. Count Complete Tree Nodes

    Question: Given a complete binary tree, count the number of nodes. Definition of a complete binary t ...

  5. LeetCode OJ:Count Complete Tree Nodes(完全二叉树的节点数目)

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

  6. 【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 ...

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

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

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

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

  9. LeetCode Count Complete Tree Nodes

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

随机推荐

  1. centos7安装chrome的历程(fedora同)

    安装 首先是下载,地址奉上:http://www.google.cn/chrome/browser/desktop/index.html,选择64 bit .rpm (适用于 Fedora/openS ...

  2. Lucene技术杂谈

    Lucene教程 1 lucene简介 1.1 什么是lucene     Lucene是一个全文搜索框架,而不是应用产品.因此它并不像www.baidu.com 或者google Desktop那么 ...

  3. 再次回首 TCP Socket服务器编程

    转载:http://www.cnblogs.com/zc22/archive/2010/06/27/1766007.html ------------------ 前言 --------------- ...

  4. VS2008 未找到编译器可执行文件 csc.exe【当网上其他方法试玩了之后不起作用的时候再用这个方法】

    被公司派遣到中国海洋石油惠州炼化公司做项目,做的是生产管理,来了发现他们的项目结构简直烂的要命,和同学们写的毕业设计差不多,然后开发工具用的是vs2008,我电脑是安装了vs2005和vs2010,v ...

  5. ios专题 - socket(1)

    二,BSD socket API 简介 BSD socket API 和 winsock API 接口大体差不多,下面将列出比较常用的 API: API接口 讲解 int socket(int add ...

  6. 阻塞式和非阻塞式IO

    有很多人把阻塞认为是同步,把非阻塞认为是异步:个人认为这样是不准确的,当然从思想上可以这样类比,但方式是完全不同的,下面说说在JAVA里面阻塞IO和非阻塞IO的区别 在JDK1.4中引入了一个NIO的 ...

  7. 生产者与消费者(三)---BlockingQueue

    前面阐述了实现生产者与消费者问题的两种方式:wait() / notify()方法 和 await() / signal()方法,本文继续阐述多线程的经典问题---生产者与消费者的第三种方式:Bloc ...

  8. TP缓存设计方案解析

    TP的缓存主要依赖Cache类,Cache类其实是一个代理类,Cache类通过getInstance静态方法来获取缓存实例,而getInstance方式实际是调用Cache类的connect方法,该方 ...

  9. Linux 共享内存编程

    共享内存允许系统内两个或多个进程共享同一块内存空间,并且数据不用在客户进程和服务器进程间复制,因此共享内存是通信速度最快的一种IPC. 实现的机制简单描述如下:一个进程在系统中申请开辟了一块共享内存空 ...

  10. marquee标签制作轮播图

    http://qy-0824.blog.163.com/blog/static/725075422011214142226/ 缺点是仅能控制轮播的速度.鼠标悬停暂停等,并不能给其指定链接.触摸滑动.分 ...