LeetCode222——Count Complete Tree Nodes
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的更多相关文章
- LeetCode222 Count Complete Tree Nodes
对于一般的二叉树,统计节点数目遍历一遍就可以了,但是这样时间复杂度O(n),一下就被卡住了. 这题首先要明白的是,我们只需要知道叶子节点的数目就能统计出总节点树. 想法1: 既然是完全二叉树,我肯定是 ...
- leetcode面试准备:Count Complete Tree Nodes
1 题目 Given a complete binary tree, count the number of nodes. In a complete binary tree every level, ...
- leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
- 完全二叉树的节点个数 Count Complete Tree Nodes
2018-09-25 16:36:25 问题描述: 问题求解: 单纯遍历了一遍,emmm,果然TLE. 解题思路就是比较左边树高度和右边树高度,如果相等,那么就是一个满二叉树,返回1 << ...
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
- 【刷题-LeetCode】222. Count Complete Tree Nodes
Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. Note: Definition ...
- LeetCode Count Complete Tree Nodes
原题链接在这里:https://leetcode.com/problems/count-complete-tree-nodes/ Given a complete binary tree, count ...
- [Swift]LeetCode222. 完全二叉树的节点个数 | Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
随机推荐
- django Modelform 使用
前言: 为什么要用form去验证呢? 我们提交的是form表单,在看前端源码时如果检查到POST URL及我们提交的字段,如果没有验证我们是否可以直接POST数据到URL,后台并没有进行校验,直接处理 ...
- 使用p6spy格式化日志输出
P6Spy 是针对数据库访问操作的动态监测框架(为开源项目,项目首 页:www.p6spy.com)它使得数据库数据可无缝截取和操纵,而不必对现有应用程序的代码作任何修改.P6Spy 分发包包括P6L ...
- 点击事件与UICollectionView的代理事件的冲突问题
发现的问题:在UIImageView上添加UICollectionView视图,点击UICollectionViewCell,不执行didSelectItemAtIndexPath的代理方法. 解决方 ...
- Codeforces Round #467 (Div. 2) B. Vile Grasshoppers[求去掉2-y中所有2-p的数的倍数后剩下的最大值]
B. Vile Grasshoppers time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Swoole MySQL 连接池的实现
目录 概述 代码 扩展 小结 概述 这是关于 Swoole 入门学习的第八篇文章:Swoole MySQL 连接池的实现. 第七篇:Swoole RPC 的实现 第六篇:Swoole 整合成一个小框架 ...
- 洛谷——P2205 [USACO13JAN]画栅栏Painting the Fence
题目描述 Farmer John has devised a brilliant method to paint the long fence next to his barn (think of t ...
- [TJOI2014] 上升子序列
刚刚做的时候一看:这不是个傻逼题吗hhhhh....然后发现写完了过不了样例,仔细一看题:同构的算一种. 这可咋办啊? 其实很简单,设f[i] 为 以a[i] 结尾的上升子序列个数,我们考虑当前如果算 ...
- 2016集训测试赛(十九)Problem A: 24点大师
Solution 这到题目有意思. 首先题目描述给我们提供了一种非常管用的模型. 按照题目的方法, 我们可以轻松用暴力解决20+的问题; 关键在于如何构造更大的情况: 我们发现 \[ [(n + n) ...
- Mac测试模拟慢网速
先普及一下Kb,KB,Kbps,Mb,Mbps等一些列概念 1Byte = 8bit 1KB (Kilobyte 千字节)=1024Byte 1MB (Megabyte,兆字节,简称“兆”)=1024 ...
- #译# Core Data概述 (转)
昨晚熬夜看发布会(本以为屌丝终于能买得起苹果了,谁知道...),因为看不了视频直播,所以就正好有空就把www.objc.io最新的一篇文章翻译了一下,同时感谢CocoaChina翻译组提供校对,以下为 ...