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. 深度学习demo

    1. Stanford Convolutional Neural Network on the MNIST digits dataset http://cs.stanford.edu/people/k ...

  2. Python3基础 list append 向尾部添加一个元素

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

  3. 0基础学安卓--初识安卓Activity

    知识储备:windows+ Android Studio 等环境安装. 安卓中Activity代表页的意思,也就是☞我们手机上当前的整个界面显示,点击按钮等操作可以跳转到另外一个Activity中. ...

  4. windows下如何安装vundle?

    参考: http://blog.csdn.net/zhuxiaoyang2000/article/details/8636472 vundle是gmarik 受 ruby的 bunler的启发开发的. ...

  5. HDU 3506 Monkey Party(区间DP)题解

    题意:有n个石堆排成环,每次能合并相邻的两堆石头变成新石堆,代价为新石堆石子数,问最少的总代价是多少 思路:先看没排成环之前怎么做:用dp[i][j]表示合并i到j所需的最小代价,那么dp[i][j] ...

  6. shiro中authc和user的权限区别

    前者(authc)是认证过,后者(user)是登录过,如果开启了rememberMe功能的话,后者(user)也是可以通过的,而前者(authc)通过不了.故我们用authc来校验一些关键操作,比如购 ...

  7. HDU 4918 Query on the subtree(动态点分治+树状数组)

    题意 给定一棵 \(n\) 个节点的树,每个节点有点权.完成 \(q\) 个操作--操作分两种:修改点 \(x\) 的点权.查询与 \(x\) 距离小于等于 \(d\) 的权值总和. \(1 \leq ...

  8. ISTQB学习笔记

    学习ISTQB大纲此文记录初次阅读时不够明确的地方 第一章:软件测试基础1. 引起软件缺陷的原因人都会犯错误(error,mistake),因此人设计的代码或文档中会引入缺陷(defect, faul ...

  9. CentOS7时间和日期的同步 (chrony和)

    CentOS 6版本,使用 hwclock CentOS 7版本,使用timedatectl 1.基本概念 1.1 GMT,UTC,CST,DST时间 世界标准时间 整个地球分为二十四时区,每个时区都 ...

  10. Centos7更改网卡名称Eth0并配置静态IP

    1.首先查看一下centos7的网卡名称 eno33554984 2.更改为centos7之前版本的网卡名称 3.更改网卡文件的名称 4.禁用可预测命名规则. 通过编辑 /etc/default/gr ...