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

则分别从左边和右边来进行传递的,当左右是完全二叉树的时候,其节点个数就是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. python面试题总结(1)

    1.python常用的数据类型. int.float.str.set.list.dict.tuple.frozenset.bool.None. 2.docstring是什么? Docstring是一种 ...

  2. 像调试java一样来调试Redis lua

    高并发的系统中,redis的使用是非常频繁的,而lua脚本则更是锦上添花.因为lua脚本本身执行的时候是一个事务性的操作,不会掺杂其他外部的命令,所以很多关键的系统节点都会用redis+lua来实现一 ...

  3. oracle分析性能问题实例

    摘录于SAP有关分析ORACLE数据性能事件的文档. 1.A check for the distribution of relevant Oracle server time revealed: 有 ...

  4. XenServer 自动化布署 (关键词: PXE ANSWER SCRIPT)

    XenServer 6.x PXE自动化布署: 测试环境:win10 + Tiny pxe server 1.0.2,采用gpxelinux.0 时间:2017.1.10 PXE远程安装: 1)def ...

  5. Java 冒泡排序法

    冒泡排序法: public static void Bubbling(int []num){//冒泡排序法 for(int i=0;inum[j+1]){//前一个大于后一个为小到大排序 前一个小于后 ...

  6. Ubuntu 18.04安装中文输入法

    reference: https://blog.csdn.net/fx_yzjy101/article/details/80243710 https://pinyin.sogou.com/linux/ ...

  7. Filebeat占用内存和CPU过高问题排查

    经反馈,新部署的服务器上filebeat占用的cpu过高,且内存只增不减. 而据我了解filebeat非常轻量级,正常情况下占用的资源几乎都能忽略不计,所以怀疑是filebeat本身出了问题. 第一时 ...

  8. 【命令】Ubuntu设置和查看环境变量

    转自[Ubuntu]Ubuntu设置和查看环境变量 查看环境变量 env env命令是environment的缩写,用于列出所有的环境变量 export 单独使用export命令也可以像env列出所有 ...

  9. NLog使用整理

    NLog使用中碰到的问题整理 1,日志写mysql数据库报错, 原因: 在sql语句中使用了mysql的函数now() 导致插入失败, 解决办法: 使用参数代替now(). 在nlog配置文件中设置  ...

  10. python3读取MySQL-Front的MYSQL密码

    python3读取MySQL-Front的MYSQL密码 python3 mysql 密码 MySQL-Front 前言 同样的套路又来了,继续尝试从配置文件中读取敏感的信息,这次轮到的是MySQL- ...