[Tree]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.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int treeDepth(TreeNode* root)
{
int dep=;
while(root){
dep++;
root=root->left;
}
return dep;
}
int countNodes(TreeNode* root) {
if(root==NULL){
return ;
}
int ldep = treeDepth(root->left);
int rdep = treeDepth(root->right);
if(ldep>rdep){
return +countNodes(root->left) + ((<<rdep)-);
}
return +((<<ldep)-) + countNodes(root->right);
}
};
[Tree]Count Complete Tree Nodes的更多相关文章
- 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 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- Java for LeetCode 222 Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- leetcode_222 Count Complete Tree Nodes
题目: Given a complete binary tree, count the number of nodes. Definition of a complete binary tree fr ...
随机推荐
- PagedList 分页
@using PagedList.Mvc;@model PagedList.IPagedList<MvcApplicationBootStramp.Models.Person> @{ ...
- 加载为应用程序池‘DefaultAppPool'提供服务的进程失败,应用程序池被禁用【解决方法】
iis应用程序池不能启动2008年03月13日 星期四 15:29iis应用程序池不能启动.WINDOWS2003 ENT SERVER(64位)环境下(测试过32位系统一样操作无此问题),当打开“应 ...
- OAuth协议与第三方登录:(QQ,百度,微信,微博)
OAuth 相当于授权的U盾,提供第三方认证的协议,是个安全相关的协议,作用在于,使用户授权第三方的应用程序访问用户的web资源,并且不需要向第三方应用程序透露自己的密码. 传统互联网:应用于PC端, ...
- UVALive 4119 Always an integer (差分数列,模拟)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Always an integer Time Limit:3000MS M ...
- C++之类和对象——C++ primer plus学习(一)
一.类的构造函数和析构函数 1.构造函数: 1)试图将类成员名称用作构造函数的参数名是错误的. 2)构造函数的参数表示的不是类成员, 而是赋给类成员的值. 3)为了避免这种混乱,一种 ...
- (原)Eclipse中将JNI生成的so打包成jar的步骤
说明:新人,对java不熟,见谅. 1. 新建工程,添加好native support,写好对应的文件(包括cpp文件,so文件和对应的java文件,此处称对应的java文件为SoJAR.java,) ...
- 一大坨GoogleAPI的学习笔记之一(oAuth2.0)
官方文档地址:https://developers.google.com/accounts/docs/OAuth2InstalledApp 最近Ubuntu下VGA接口无端的不能用了,木有心情翻译了, ...
- Js 导出Excel IE ActiveX控件
function ExportExcel() { var oXL = new ActiveXObject("Excel.Application"); //创建excel应用程序对象 ...
- 手机扫描二维码下载APP,根据操作系统不同自动下载
Android和IOS手机扫描二维码下载APP,根据OS不同,自动处理相应下载操作.IOS自动跳转至AppStore应用下载页,Android自动下载应用的apk包. <script type= ...
- wordpress教程之the_author_meta()显示用户的信息
描述 模板标签函数the_author_meta可以显示用户数据.如果该函数在文章主循环(Loop)中,则不必指定作者的ID值,标签所显示的就是当前文章作者的内容.如果在主循环(Loop)外,则需要指 ...