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.
数二叉树的节点数首先肯定是可以用暴力解法去解问题的,但是这样总是会timeout:

 class Solution {
public:
int countNodes(TreeNode* root) {
if(!root) return ;
total++;
countNodes(root->left);
countNodes(root->right);
return total;
}
private:
int total;
};

其他的办法就是对完全二叉树而言,其一直向左走和一直向右边、走只可能是相同的或者相差1,如果相同那么起节点个数就是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) {
if(!root) return ;
TreeNode * leftNode = root;
TreeNode * rightNode = root;
int left = ;
int right = ;
while(leftNode->left){
left++;
leftNode=leftNode->left;
}
while(rightNode->right){
right++;
rightNode = rightNode->right;
}
if(left == right) return ( << (left + )) - ;
else return + countNodes(root->left) + countNodes(root->right);
}
};

写的比较乱哈 ,凑合着看看把。

下面是java代码,首先肯定是递归形式的,不出所料,同样会TLE:

 /**
* 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;
else
return 1 + countNodes(root.left) + countNodes(root.right);
}
}

考虑到完全二叉树的性质,下面这个写法就不会TLE了:

 public class Solution {
public int countNodes(TreeNode root) {
if(root == null)
return 0;
int right = 1, left = 1;
TreeNode leftNode = root;
TreeNode rightNode = root;
while(leftNode.left != null){
leftNode = leftNode.left;
left++;
}
while(rightNode.right != null){
rightNode = rightNode.right;
right++;
}
if(left == right)
return (1 << left) - 1;
else
return 1 + countNodes(root.left) + countNodes(root.right); //注意这里不要忘记计算本身的这个节点
}
}

LeetCode OJ:Count Complete Tree Nodes(完全二叉树的节点数目)的更多相关文章

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

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

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

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

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

  4. leetcode之Count Complete Tree Nodes

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

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

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

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

  7. 222 Count Complete Tree Nodes 完全二叉树的节点个数

    给出一个完全二叉树,求出该树的节点个数.完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置.若最底层为第 h ...

  8. leetcode 222.Count Complete Tree Nodes

    完全二叉树是从左边开始一点点填充节点的,因此需要计算所有的节点的个数. 则分别从左边和右边来进行传递的,当左右是完全二叉树的时候,其节点个数就是pow(2,h)-1. /** * Definition ...

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

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

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

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

随机推荐

  1. (扫盲)WebSocket 教程

    原文地址:http://www.ruanyifeng.com/blog/2017/05/websocket.html WebSocket 是一种网络通信协议,很多高级功能都需要它. 本文介绍 WebS ...

  2. PowerDesigner数据模型(CDM—PDM)

    操作过程 点击:  Tools/Generate Physical Data Model

  3. python有哪些好的学习资料或者博客?

    推荐Full Stack Python 有各种python资源汇总,从基础入门到各种框架web应用开发和部署,再到高级的ORM.Docker都有.以下是Full Stack Python 上总结的一些 ...

  4. PsySH——PHP交互式控制台

    PsySH PsySH is a runtime developer console, interactive debugger and REPL for PHP. PsySH是一个PHP的运行时开发 ...

  5. iOS 什么是函数式编程

    前言:当前只做理解性的常规背书,根据不断深入学习会不断丰富解读内容,欢迎评论提意见 函数式编程:Functional Programming 1 基本解释: 函数式编程 是一种思维模式,一种编程思想, ...

  6. VC 取消warning

    #pragma warning (disable:4200) 4200是指具体哪个warning

  7. $《利用Python进行数据分析》学习笔记系列——IPython

    本文主要介绍IPython这样一个交互工具的基本用法. 1. 简介 IPython是<利用Python进行数据分析>一书中主要用到的Python开发环境,简单来说是对原生python交互环 ...

  8. Linux串口编程(中断方式和select方式)

    Linux下的串口编程,在嵌入式开发中占据着重要的地位,因为很多的嵌入式设备都是通过串口交换数据的.在没有操作系统的我们可以使用UART的中断来出来数据的接受和发送,而在Linux操作系统下,我们也可 ...

  9. 0802 DRF 视图

    昨日回顾: 1. Serializer(序列化) 1. ORM对应的query_set和ORM对象转换成JSON格式的数据 1. 在序列化类中定义自定义的字段:SerializerMethodFiel ...

  10. verilog FAQ(zz)

    1. What is the race condition in verilog? Ans :The situation when two expressions are allowed to exe ...