Total Accepted: 32628 Total Submissions: 129569 Difficulty: Medium

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

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.

如果用暴力递归来计算节点总数,那么会超时,时间复杂度是O(N)。由于我们求的是完全二叉树的节点数,如果用求普通二叉树的方法来解决肯定是不合适的。完全二叉树的一些特点可能成为我们解决该问题的思路:如果一个节点的左子树的深度和右子树的深度一样,那么以该节点为根节点的树的节点数为:h2-1。最好情况下的时间复杂度为O(h),其它情况的时间复杂度为O(h2)。代码如下:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int countNodes(TreeNode root) {
if(root==null) return 0; int l = getLeft(root) + 1;
int r = getRight(root) + 1; if(l==r) {
return (2<<(l-1)) - 1;
} else {
return countNodes(root.left) + countNodes(root.right) + 1;
}
} private int getLeft(TreeNode root) {
int count = 0;
while(root.left!=null) {
root = root.left;
++count;
}
return count;
} private int getRight(TreeNode root) {
int count = 0;
while(root.right!=null) {
root = root.right;
++count;
}
return count;
}
}
 

LeetCode OJ 222. Count Complete Tree Nodes的更多相关文章

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

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

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

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

  3. 【Leetcode】222. Count Complete Tree Nodes

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

  4. LeetCode OJ:Count Complete Tree Nodes(完全二叉树的节点数目)

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

  5. 【leetcode】222. Count Complete Tree Nodes(完全二叉树)

    Given the root of a complete binary tree, return the number of the nodes in the tree. According to W ...

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

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

  7. leetcode面试准备:Count Complete Tree Nodes

    1 题目 Given a complete binary tree, count the number of nodes. In a complete binary tree every level, ...

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

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

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

随机推荐

  1. python生产者消费者模型

    业界用的比较广泛,多线程之间进行同步数据的方法,解决线程之间堵塞,互相不影响. server --> 生产者 client --> 消费者 在一个程序中实现又有生产者又有消费者 ,生产者不 ...

  2. CSS3入门

    CSS3 w3cschools css3  MDN英文  MDN中文 CSS3 is the latest evolution of the Cascading Style Sheets langua ...

  3. Python简记

    1.字符换行: print('ab \ncd \nef')

  4. Ubuntu 14.0 升级内核到指定版本

    1.卸载现有内核sudo apt purge linux-headers-* linux-headers-*-generic linux-image-*-generic linux-image-ext ...

  5. MarkDown初遇

    MarkDown初遇 纠结盘桓许久,由于那只胖纸,最终决定再次捡起博客这个东东,记录记录生活中.心灵里的点点滴滴. 寻觅的过程中忽然发现MarkDown这个东东,查了查,学习成本不高,简洁而标准,关键 ...

  6. MarkDown编写规范指南

    Markdown 编写规范指南 简介: Markdown的目标是实现「易读易写」,成为一种适用于网络的「书写语言」. 一份使用Markdown格式撰写的文件可以直接以纯文本发布,它的最大灵感来源其实是 ...

  7. MVC模式学习笔记

    一.如何设计一个程序的结构,这是一门专门的学问,叫做“架构模式”(architectural pattern),属于编程的方法论. MVC模式就是架构模式的一种,我觉得它不仅适用于开发软件,也适用于其 ...

  8. 12款免费与开源的NoSQL数据库

    Naresh Kumar是位软件工程师与热情的博主,对于编程与新事物拥有极大的兴趣,非常乐于与其他开发者和程序员分享技术上的研究成果.近日,Naresh撰文谈到了12款知名的免费.开源NoSQL数据库 ...

  9. HDU 1532 最大流入门

    1.HDU 1532 最大流入门,n个n条边,求第1点到第m点的最大流.只用EK做了一下. #include<bits/stdc++.h> using namespace std; #pr ...

  10. [妙味DOM]第三课:Event-事件详解1

    知识点总结 焦点事件 onfocus 获取焦点 onblur 失点焦点 obj.focus() 给指定元素设置焦点 obj.blur() 取消指定元素的焦点 obj.select() 选择指定元素里的 ...