完全二叉树是从左边开始一点点填充节点的,因此需要计算所有的节点的个数。

则分别从左边和右边来进行传递的,当左右是完全二叉树的时候,其节点个数就是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的更多相关文章

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

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

  2. 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 ...

  3. (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 ...

  4. 【刷题笔记】LeetCode 222. Count Complete Tree Nodes

    题意 给一棵 complete binary tree,数数看一共有多少个结点.做题链接 直观做法:递归 var countNodes = function(root) { if(root===nul ...

  5. [leetcode]222. Count Complete Tree Nodes完全二叉树的节点数

    /* 满二叉树的特点是2^n-1,对于完全二叉树,一个node如果左右子树深度相同,那么 是一个满二叉树.如果不是,那就把node算上,继续往下看,下边的可能是满二叉树 由于完全二叉树中有一些子满二叉 ...

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

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

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

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

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

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

  9. LeetCode OJ 222. Count Complete Tree Nodes

    Total Accepted: 32628 Total Submissions: 129569 Difficulty: Medium Given a complete binary tree, cou ...

随机推荐

  1. dot.js模板实现分离式

    <!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...

  2. 【webpack学习笔记】a08-缓存

    一般要优化网站的加载速度可以从缓存入手,利用缓存可以降低网络流量,减少访问请求,从而可以达到加快访问速度的目的. 但是,如果一旦将文件缓存,那么有更新的时候就不能及时更新,若要看到最新内容,那么就需要 ...

  3. Tasklist使用详解

    用jstat查看jvm内存的使用的情况时,因为是windows机器,不能使用top命令方便的查出来,进程好在网上搜了一下看到了在windows原来使用的是tasklist 特意将tasklist的用法 ...

  4. 使用idea启动springMVC+Hibernate其他项目

    打开项目后打开Project Structure 点开左边的Libraries 加入依赖包 点开左边的Moudules 选中项目 新建Web,Spring,Hibernate三项 Hibernate添 ...

  5. Bouml快速使用指南

    一.Bouml简介 Android 系统中有大量Java.C++代码,继承以及依赖关系非常复杂,Bouml可以用c++.Java.Idl.Php和Python建模及生成代码,反之也可通过uml工具更好 ...

  6. java的http请求实例

    package vqmp.data.pull.vqmpull.common.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory ...

  7. 实训任务01:安装Hadoop

    实训任务1:安装Hadoop 实训1 :为Hadoop集群增加一个节点 需示说明: 运行环境:操作系统:centos6.8 ,hadoop2.6.4 在实训指导中搭建了3个节点的hadoop集群,要求 ...

  8. dom编程艺术笔记2--第三章

    DOM 1.dom:简单理解为文档对象模型<==>节点树 2.宿主对象:浏览器提供的对象—>document对象 3. html标签为树顶点元素 head body为html子元素以 ...

  9. Oracle数据库各种名字的区别

    数据库名(DB_NAME).数据库实例名(INSTANCE_NAME).操作系统环境变量(ORACLE_SID).数据库服务名(SERVICE_NAME).数据库域名(DB_DOMAIN)以及全局数据 ...

  10. socket.error: [Errno 99] Cannot assign requested address

    方法一:python 命令行下运行  vi /etc/hosts 将127.0.1.1 那一行的名字改成你的(用 vi /etc/hostname 获取) 127.0.0.1 localhost 12 ...