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

Note:

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 2h nodes inclusive at the last level h.

Example:

Input:
1
/ \
2 3
/ \ /
4 5 6 Output: 6
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int countNodes(TreeNode root) {
int left = leftCount(root);
int right = rightCount(root);
if (left != right) {
return 1 + countNodes(root.left) + countNodes(root.right);
} else {
return (1 << left) - 1;
}
} private int leftCount(TreeNode cur) {
int res = 0;
while (cur != null) {
res += 1;
cur = cur.left;
}
return res;
} private int rightCount(TreeNode cur) {
int res = 0;
while (cur != null) {
res += 1;
cur = cur.right;
}
return res;
}
}

[LC] 222. Count Complete Tree Nodes的更多相关文章

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

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

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

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

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

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

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

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

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

  6. 222. Count Complete Tree Nodes

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

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

  8. LeetCode OJ 222. Count Complete Tree Nodes

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

  9. 【Leetcode】222. Count Complete Tree Nodes

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

随机推荐

  1. {转} MJPG流媒体在HTML5的呈现方案

    最近碰到的需求:监控探头视频呈现到html页面上. 视频源协议:HLS; 视频源格式:Motion JPEG 简称 MJPG; 其中Motion JPEG(M-JPEG或MJPEG,Motion Jo ...

  2. 初学者学习JavaScript的实用技巧!

    Javascript是一种高级编程语言,通过解释执行.它是一门动态类型,面向对象(基于原型)的直译语言.它已经由欧洲电脑制造商协会通过ECMAScript实现语言标准化,它被世界上的绝大多数网站所使用 ...

  3. 吴裕雄--天生自然 JAVASCRIPT开发学习: 表单

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> ...

  4. UIWindow statusBar消失

    1.新建UIWindow 程序崩溃 报无根控制器错误 Xcode7环境下,新建UIWindow需添加rootViewController 2.新建UIWindow后 statusBar消失 Info. ...

  5. Mybatix实现in查询(五)

    在这一节,我们要向大家介绍一下在Mybatis中想要实现in查询,Mapper文件应该怎么配置. 1)在com.mybatis.dao.PartDao中增加接口函数 public List<Pa ...

  6. 十六进制转化二进制[c]

    #include<stdio.h> #include<string.h> #include<stdlib.h> int hex2dec(char c) { ; // ...

  7. 14 微服务电商【黑马乐优商城】:day01-springboot(理论篇)

    本项目的笔记和资料的Download,请点击这一句话自行获取. day01-springboot(理论篇) :day01-springboot(实践篇) :day01-springboot(Thyme ...

  8. Cocoapod-终端

    安装循序: Xcode->RVM->Ruby(安装过程中需要安装homebrew)->CocoaPats 参考文章: 安装地址:http://www.cnblogs.com/dagu ...

  9. Mysql 环境部署

    1.Window 1.1 下载软件: https://dev.mysql.com/downloads/mysql/ 依次点击上图 红色框中按钮 1.2 安装软件 1.2.1 解压软件 正常解压即可  ...

  10. sklearn KMeans聚类算法(总结)

    基本原理 Kmeans是无监督学习的代表,没有所谓的Y.主要目的是分类,分类的依据就是样本之间的距离.比如要分为K类.步骤是: 随机选取K个点. 计算每个点到K个质心的距离,分成K个簇. 计算K个簇样 ...