2018-09-25 16:36:25

问题描述:

问题求解:

单纯遍历了一遍,emmm,果然TLE。

解题思路就是比较左边树高度和右边树高度,如果相等,那么就是一个满二叉树,返回1 << h - 1即可,如果不是,则递归的计算左右子树的个数。

时间复杂度:O(logn * logn)

    public int countNodes(TreeNode root) {
if (root == null) return 0;
int l = leftHeight(root);
int r = rightHeight(root);
if (l == r) return (1 << l) - 1;
else return 1 + countNodes(root.left) + countNodes(root.right);
} private int leftHeight(TreeNode root) {
if (root == null) return 0;
return 1 + leftHeight(root.left);
} private int rightHeight(TreeNode root) {
if (root == null) return 0;
return 1 + rightHeight(root.right);

2019-04-15 14:42:44

其实我根本不用管二叉树的种类,直接递归去计数就可以了,在log(n)的时间复杂度内完成,且速度完胜之前的解法。

Runtime: 0 ms, faster than 100.00% of Java online submissions for Count Complete Tree Nodes.

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

完全二叉树的节点个数 Count Complete Tree Nodes的更多相关文章

  1. [Swift]LeetCode222. 完全二叉树的节点个数 | Count Complete Tree Nodes

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

  2. 【LeetCode 222_完全二叉树_遍历】Count Complete Tree Nodes

    解法一:递归 int countNodes(TreeNode* root) { if (root == NULL) ; TreeNode *pLeft = root->left; TreeNod ...

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

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

  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

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

  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. [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数

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

  9. 222. Count Complete Tree Nodes -- 求完全二叉树节点个数

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

随机推荐

  1. centos6.5安装部署zabbix监控服务端和客户端

    部署zabbix服务端需要LNMP环境(nginx,mysql,php),其它数据库也可以,我这里使用mysql,关于LNMP环境部署,可以参考我的另一遍文章:http://www.cnblogs.c ...

  2. luogu 3790 文艺数学题 - 矩阵树定理 - 容斥原理

    题目传送门 戳我来传送 题目大意 给定一个图,问它的所有生成树的边权的最大公约数之和. 可以考虑计算边权的最大公约数为$i$的生成树的个数$f(i)$,最后累加答案. 然后考虑这样的生成树的个数怎么求 ...

  3. 1. 元信息:Meta类 2. 基于对象查询的sql优化 3. 自定义:Group_Concat() 4. ajax前后台交互

    一.元信息 ''' 1. 元信息 1. Model类可以通过元信息类设置索引和排序信息 2. 元信息是在Model类中定义一个Meta子类 class Meta: # 自定义表名 db_table = ...

  4. 针对Xcode 9 + iOS11 的修改,及iPhone X的适配

    1,UIScrollView的automaticallyAdjustsScrollViewInsets 失效了. automaticallyAdjustsScrollViewInsets,当设置为YE ...

  5. Logger级别和输出的地方

    转载自http://blog.csdn.net/u014756827/article/details/52475990 log4j日志配置 关键字: apache log4j 1.配置根Logger: ...

  6. javascript中的高阶函数, 和 类定义Function, 和apply的使用

    参考: http://www.cnblogs.com/delin/archive/2010/06/17/1759695.html js中的类, 也是用function关键字来定义的: function ...

  7. hdu1358 Period kmp求循环节

    链接 http://acm.hdu.edu.cn/showproblem.php?pid=1358 思路 当初shenben学长暑假讲过,当初太笨了,noip前几天才理解过来.. 我也没啥好说的 代码 ...

  8. 振兴中华|2013年蓝桥杯A组题解析第三题-fishers

    标题: 振兴中华 小明参加了学校的趣味运动会,其中的一个项目是:跳格子. 地上画着一些格子,每个格子里写一个字,如下所示:(也可参见p1.jpg) 从我做起振 我做起振兴 做起振兴中 起振兴中华 比赛 ...

  9. POJ 3041 Asteroids(最小点覆盖)题解

    题意:n*n的网格中有k个点,开一枪能摧毁一行或一列的所有点,问最少开几枪 思路:我们把网格看成两个集合,行集合和列集合,如果有点x,y那么就连接x->y,所以我们只要做最小点覆盖就好了. 参考 ...

  10. BFS广度优先 vs DFS深度优先 for Binary Tree

    https://www.geeksforgeeks.org/bfs-vs-dfs-binary-tree/ What are BFS and DFS for Binary Tree? A Tree i ...