222. Count Complete Tree Nodes -- 求完全二叉树节点个数
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 -- 求完全二叉树节点个数的更多相关文章
- [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- 【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 ...
- leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
- 【刷题-LeetCode】222. Count Complete Tree Nodes
Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. Note: Definition ...
- 222 Count Complete Tree Nodes 完全二叉树的节点个数
给出一个完全二叉树,求出该树的节点个数.完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置.若最底层为第 h ...
- 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 ...
- LeetCode OJ:Count Complete Tree Nodes(完全二叉树的节点数目)
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
随机推荐
- PL/SQL编程基础(二):变量的声明、赋值、(赋值、连接、关系、逻辑)运算符
变量的声明.赋值.运算符 1.声明并使用变量 变量可以在声明时赋值,也可以先定义后赋值: 使用%TYPE与%ROWTYPE可以根据已有类型定义变量. PL/SQL是一种强类型的编程语言,所有的变量都必 ...
- HDU_5521_Meeting
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- Fast and Accurate Traffic Matrix Measurement Using Adaptive Cardinality Counting
paper-CaiPan.pdf http://conferences.sigcomm.org/sigcomm/2005/paper-CaiPan.pdf
- Python开发【模块】:Requests(一)
Requests模块 1.模块说明 Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写,真正的为人类着想. Python 标准库中的 urlli ...
- go-002-语言结构
Go 语言的基础组成有以下几个部分: 包声明package,必须在源文件中非注释的第一行指明这个文件属于哪个包, 引入包import,在开头部位使用 import 导入包,单个包 import “fm ...
- mysql创建和删除表
创建表 简单的方式 CREATE TABLE person ( ), name ), birthday DATE ); 或者是 CREATE TABLE IF NOT EXISTS person ( ...
- Django model中数据批量导入bulk_create()
在Django中需要向数据库中插入多条数据(list).使用如下方法,每次save()的时候都会访问一次数据库.导致性能问题: for i in resultlist: p = Account(nam ...
- mydumper原理介绍
mydumper的安装:http://www.cnblogs.com/lizhi221/p/7010174.html mydumper介绍 MySQL自身的mysqldump工具支持单线程 ...
- python学习之路-day8
一.接口与归一化设计 1.什么是接口 调用某个功能的方法/方式/入口 2.为什么要用接口 接口提取了一群类共同的函数,可以把接口当做一个函数的集合. 然后让子类去实现接口中的函数. 这么做的意义在于归 ...
- (转)SQL Server 2008登录错误:无法连接到(local)的解决
在一些朋友安装完SQL Server 2008之后大多会遇到连接出错的问题.特别对于我们这样的新手而言简直郁闷的要死,好不容易装玩了又出现了问题.此篇文章意在解决安装步骤没有问题,但安装后无法登录的问 ...