leetcode 222.Count Complete Tree Nodes
完全二叉树是从左边开始一点点填充节点的,因此需要计算所有的节点的个数。
则分别从左边和右边来进行传递的,当左右是完全二叉树的时候,其节点个数就是pow(2,h)-1。
/**
* 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 countNodes(TreeNode* root) {
int hleft=, hright=;
TreeNode* pleft=root, *pright=root;
while(pleft){
hleft++;
pleft=pleft->left;
}
while(pright){
hright++;
pright=pright->right;
}
if(hleft==hright) return pow(,hleft)-;
return countNodes(root->left)+countNodes(root->right)+;
}
};
leetcode 222.Count Complete Tree Nodes的更多相关文章
- [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- 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 ...
- (medium)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
题意 给一棵 complete binary tree,数数看一共有多少个结点.做题链接 直观做法:递归 var countNodes = function(root) { if(root===nul ...
- [leetcode]222. Count Complete Tree Nodes完全二叉树的节点数
/* 满二叉树的特点是2^n-1,对于完全二叉树,一个node如果左右子树深度相同,那么 是一个满二叉树.如果不是,那就把node算上,继续往下看,下边的可能是满二叉树 由于完全二叉树中有一些子满二叉 ...
- leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
- 【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 OJ 222. Count Complete Tree Nodes
Total Accepted: 32628 Total Submissions: 129569 Difficulty: Medium Given a complete binary tree, cou ...
随机推荐
- 简单的压力测试工具 siege
$ siege -c 1000 -r 100 -b http://127.0.0.1:13579/3344 HTTP/1.0 200 0.02 secs: 49 bytes ==> GET /3 ...
- 面向对象编程思想(OOP)
本文我将从面向对象编程思想是如何解决软件开发中各种疑难问题的角度,来讲述我们面向对象编程思想的理解,梳理面向对象四大基本特性.七大设计原则和23种设计模式之间的关系. 软件开发中疑难问题: 软件复杂庞 ...
- 用户认证:基于jwt和session的区别和优缺点
背景知识: Authentication和Authorization的区别: Authentication:用户认证,指的是验证用户的身份,例如你希望以小A的身份登录,那么应用程序需要通过用户名和密码 ...
- 关于FGPA的复位
关于FGPA的复位 当初开始学FPGA的时候,总是疑惑:FPGA不是没有复位管教么,但总在always看到有复位信号.这个复位信号(我们暂且称为rst_n)从哪里来? 实际上是可以从两个方面获得的,这 ...
- 使用junit和eclemma进行简单的代码测试
1.Junit和Hamcrest的安装 可以在https://mvnrepository.com/上面下载所需要的Junit和Hamcrest的jar包,然后在项目中新建一个lib文件夹,将下载好的j ...
- 003dayPython学习初始模块和字节码
一.注释: 1.单行注释 # 被注释的内容 2.多行注释 """ 被注释的内容 """ 二.模块 我们在编程的时候,往往是一个主.py文件, ...
- java——形参与实参
看了很多的文章,稍微有一些的总结:对最基本的形参与实参有了一定的理解,虽然还是不够深入. 1.基本概念 形参:全称为"形式参数"是在定义函数名和函数体的时候使用的参数,目的是用来接 ...
- vue-cli title 里面怎动态显示文字
在路由里每个都添加一个meta[{ path:'/login', meta: { title: '登录页面' }, component:'login' }] main.js里面加如下代码: ...
- 关于MySQL什么时候使用索引问题以及什么情况下应不建或少建索引
一,什么情况下使用索引1. 表的主关键字 自动建立唯一索引 2. 表的字段唯一约束 ORACLE利用索引来保证数据的完整性 3. 直接条件查询的字段 在SQL中用于条件约束的字段 如zl_yhjbqk ...
- ie9上传后下载json
1.保持后台控制器返回的数据为字符串格式 2.js:dataType类型保持为html格式 dataType: 'html',//默认就是html类型,不写对火狐有影响 3.将上传后后台返回的字符串转 ...