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. Summarization of Tech Interviews

    Summarization of Tech Interviews(技术面试总结) 手心网(2015) Q1. 解释一下 TCP/IP 协议之滑动窗口? 滑动窗口协议的维基:https://en.wik ...

  2. mustache 模板使用

    //一 ,基本使用 <!DOCTYPE html><html ng-app="myApp"><head lang="en"> ...

  3. 20155201 网络攻防技术 实验九 Web安全基础

    20155201 网络攻防技术 实验九 Web安全基础 一.实践内容 本实践的目标理解常用网络攻击技术的基本原理.Webgoat实践下相关实验. 二.报告内容: 1. 基础问题回答 1)SQL注入攻击 ...

  4. Centos7.03搭建JDK、Tomcat、MySql环境

    本人linux服务器配置是:centos_7_03_64_20G_xdragon_20171025.vhd 公网IP:106.14.14.224 内存:2GB SecureCRT8.0:https:/ ...

  5. 为什么call比apply快

    这是一个非常有意思的问题. 在看源码的过程中,总会遇到这样的写法: var triggerEvents = function(events, args) { var ev, i = -1, l = e ...

  6. Python带_的变量或函数命名

    python中的标识符可以包含数字.字母和_,但必须以字母或者_开头,其中以_开头的命名一般具有特殊的意义. 前后均带有双下划线__的命名 一般用于特殊方法的命名,用来实现对象的一些行为或者功能,比如 ...

  7. 【Python046--魔法方法:描述符】

    一.描述符的定义: 描述符就是将特殊类型的类的实例指派给另外一个类的属性 1.举例: 特殊类型的类要实现以下三个方法中的其中一个或者全部实现 * __get__(self,instance,owner ...

  8. Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password

    Centos7.5 执行ansible命令报错 问题: [root@m01 ~]# ansible servers -a "hostname" -i ./hosts -u root ...

  9. 探究is与==的区别

    1.is  和 ==的区别: 主要参考内存地址: 部分字符串和数字有固定的小数据池: 比如: a="abc" a1="abc" print(id(a),id(a ...

  10. Python3 tkinter基础 Entry show textvariable 密码输入框

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...