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

则分别从左边和右边来进行传递的,当左右是完全二叉树的时候,其节点个数就是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. lr12脚本参数化

    1.注册.登录.获取商品列表lr脚本 Action() {        lr_save_string("192.168.46.129:8080","IP"); ...

  2. react球形文字旋转标签

    /* * 球形文字旋转标签模块 * */ import React, {Component, PropTypes} from "react"; import ReactDOM fr ...

  3. Tensorflow常用的函数:tf.cast

    1.tf.cast(x,dtype,name) 此函数的目的是为了将x数据,准换为dtype所表示的类型,例如tf.float32,tf.bool,tf.uint8等 example:  import ...

  4. npm安装material-design-icons总是失败

    项目中使用npm或者cnpm安装material-design-icons总是失败 解决办法: 1.自己上github下载后拷贝到项目node_modules目录下 2.还有npm安装老出问题,npm ...

  5. RockerMQ实战之快速入门

    文章目录 RocketMQ 是什么 专业术语 Producer Producer Group Consumer Consumer Group Topic Message Tag Broker Name ...

  6. flink基础教程读书笔记

    数据架构设计领域发生了重大的变化,基于流的处理是变化的核心. 分布式文件系统用来存储不经常更新的数据,他们也是大规模批量计算所以来的数据存储方式. 批处理架构(lambda架构)实现计数的方式:持续摄 ...

  7. android spf中存list<string>

    private void setSpfList() { final SharedPreferences mSharedPreferences = getPreferences(Context.MODE ...

  8. PAT A1103

    PAT A1103 标签(空格分隔): PAT 解题思路: DFS #include <cstdio> #include <vector> using namespace st ...

  9. tcp的粘包现象与解决方案

    粘包现象: 粘包1:连续的小包,会被优化机制给合并 粘包2:服务端一次性无法完全就收完客户端发送的数据,第二再次接收的时候,会接收到第一次遗留的内容 模拟一个粘包现象 服务端 import socke ...

  10. Java - Java入门(2-1am)

    第一讲.Java入门 1. 计算机语言是人和计算机进行交互的一种工具,人们通过使用计算机语言编写程序来向计算机施令,计算机则执行程序,并把结果输出给用户. 2. 机器语言:由0.1序列构成的指令码组成 ...