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.

1. 递归

//return -1 if it is not.
int isCompleteTree(TreeNode* root) {
if (!root) return ; int cnt = ;
TreeNode *left = root, *right = root;
for(; left && right; left=left->left, right=right->right) {
cnt *= ;
} if (left!=NULL || right!=NULL) {
return -;
}
return cnt-;
} int countNodes(TreeNode* root) {
int cnt = isCompleteTree(root);
if (cnt != -) return cnt;
int leftCnt = countNodes(root->left);
int rightCnt = countNodes(root->right);
return leftCnt + rightCnt + ;
}

2. 非递归

/**
* 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 ans = , lh = , rh = ;
TreeNode *p = root;
while(p)
{
//先求p的左右子树的最大深度。若相等,则p加左子树节点个数为1<<lh,再求右子树;若不等,则p加右子树节点个数为1<<rh,再求左子树。
if(!lh) //若lh不为0,说明lh由之前的lh--得到,是已知的。
{
for(TreeNode *t = p->left; t; t = t->left)
lh++;
}
for(TreeNode *t = p->right; t; t = t->left)
rh++;
if(lh == rh)
{
ans += << lh;
p = p->right;
}
else
{
ans += << rh;
p = p->left;
}
lh--;
rh = ;
}
return ans;
}
};

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. [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 使用DBMS_SUPPORT包

    DBMS_SUPPORT包从Oracle7.2引入,主要功能用以提供更完整的用户session跟踪信息,这个包可以通过运行 dbmssupp.sql 和 prvtsupp.plb 两个脚本来创建.该脚 ...

  2. 前端~HTML~CSS~JavaScript~JQuery~Vue

    HTML CSS JavaScript DOM文档操作 jQuery实例 Vue

  3. 用MongoDB取代RabbitMQ(转)

    原文:http://blog.nosqlfan.com/html/3223.html RabbitMQ是当成应用比较广泛的队列服务系统,其配套的客户端和监控运维方案也比较成熟.BoxedIce的队列服 ...

  4. SQL基础--查询之四--集合查询

    SQL基础--查询之四--集合查询

  5. SaltStack系列(四)之实例编写

    前面已经介绍的够多了,这里来让我们写一些完整的实例来梳理一下. 强调一下,sls文件的抒写格式都是"-"后面跟一个空格,然后后面跟参数: 然后后面再跟一个空格,然后是要填写的值.但 ...

  6. DataFrames与RDDs的相互转换

    Spark SQL支持两种RDDs转换为DataFrames的方式 使用反射获取RDD内的Schema     当已知类的Schema的时候,使用这种基于反射的方法会让代码更加简洁而且效果也很好. 通 ...

  7. Java基础知识Set、List、Map的区别

    就学习经验,浅谈Java中的Set,List,Map的区别,对JAVA的集合的理解是相对于数组: 数组是大小固定的,并且同一个数组只能存放类型一样的数据(基本类型/引用类型),JAVA集合可以存储和操 ...

  8. CSS控制列表样式属性list-style有哪些?怎么用?

    CSS列表样式属性list-style有哪些类型?不同类型CSS控制列表样式使用时该注意什么? 这是W3Cschool用户Shirley于2016-11-10在W3Cschool编程问答提出的问题.云 ...

  9. VS2010/MFC编程入门之七(对话框:为对话框添加控件)

    创建对话框资源需要创建对话框模板.修改对话框属性.为对话框添加各种控件等步骤,前面一讲中鸡啄米已经讲了创建对话框模板和修改对话框属性,本节继续讲如何为对话框添加控件. 上一讲中鸡啄米创建了一个名为“A ...

  10. uwsgi手动安装时报错ValueError: invalid literal for int() with base 10: '32_1'

    安装uwsgi,安装步骤如下 wget https://projects.unbit.it/downloads/uwsgi-latest.tar.gz tar zxvf uwsgi-latest.ta ...