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

Definition of a complete binary tree from Wikipedia:

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 2hnodes
inclusive at the last level h.

实现:

class Solution {

public:

    int getHeight(TreeNode* root) {

        int h = 0;

        while (root) {

            root = root->left;

            h++;

        }

        return h;

    }

    int countNodes(TreeNode* root) {

        if (!root) return 0;

        int lh = getHeight(root->left);

        int rh = getHeight(root->right);

        

        if (lh == rh) 

            return pow(2, lh) + countNodes(root->right);

        return pow(2, rh) + countNodes(root->left);

    }

};

LeetCode222——Count Complete Tree Nodes的更多相关文章

  1. LeetCode222 Count Complete Tree Nodes

    对于一般的二叉树,统计节点数目遍历一遍就可以了,但是这样时间复杂度O(n),一下就被卡住了. 这题首先要明白的是,我们只需要知道叶子节点的数目就能统计出总节点树. 想法1: 既然是完全二叉树,我肯定是 ...

  2. leetcode面试准备:Count Complete Tree Nodes

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

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

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

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

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

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

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

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

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

  7. LeetCode Count Complete Tree Nodes

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

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

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

  9. [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数

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

随机推荐

  1. 常用 vim 命令总结

    学习 vim ,是需要经常使用的,而这些命令,是我这段时间最常用的命令,很有效率的提高的我的文本编辑效率.----------------------------------------------- ...

  2. 使用中科大源下载android源码

    我的系统时manjaro linux 最新版的,安装过了git和curl软件 一.如果没有安装的同学,请使用命令: pacman -S git curl 我这里安装了python3和python2,但 ...

  3. Scrapy笔记:日志的使用

    scrapy的日志记录有两种方式: spider.logger.xx()和python标准库中的logger = logging.get_Logger('log information') 向日志对象 ...

  4. mariadb 集群使用

    集群启动问题 在kvm虚机下,启动mariad,日志报如下错误: :: [Note] /usr/libexec/mysqld: Shutdown complete :: mysqld_safe mys ...

  5. LeetCode OJ--Anagrams **

    https://oj.leetcode.com/problems/anagrams/ 在一个vector<string>中,找到所有经过顺序变换,可以变成一样的 string. 首先,对每 ...

  6. codevs——3372 选学霸(背包)

    题目等级 : 大师 Master  时间限制: 1 s  空间限制: 128000 KB 题解       题目描述 Description 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实 ...

  7. iptables 一些有用的规则

      -A INPUT -i lo -j ACCEPT #允许本机内部访问,即回环 -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT #允许 ...

  8. 基于WPF系统框架设计(8)-PasswordBox传值到ViewMode

    应用场景 我要做一个系统登录功能,需要传用户名和密码到ViewModel中,可是PasswordBox传值到ViewModel中好像跟TextBox等控件不一样.这里需要用到附加属性. 附加属性:一个 ...

  9. 在Bonobo服务器里创建Repository(库)

    新建Repository步骤如下: 点击“库”链接,进入“库管理”页面,如下图所示: 在“库管理”页面点击“创建新库”按钮,进入“创建新库”页面,如下图所示: 点击“建立”按钮,会进入“库管理”页面, ...

  10. innodb事务锁

    计算机程序锁   控制对共享资源进行并发访问 保护数据的完整性和一致性   lock  主要是事务,数据库逻辑内容,事务过程 latch/mutex 内存底层锁:   更新丢失 原因: B的更改还没有 ...