【刷题-LeetCode】222. Count Complete Tree Nodes
- Count Complete Tree Nodes
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
解法1 一般的计算节点数目方法,count(root) = 1 + count(root->left) + count(root->left)
class Solution {
public:
int countNodes(TreeNode* root) {
if(root == NULL)return 0;
return 1 + countNodes(root->left) + countNodes(root->right);
}
}
解法2 先序/中序/后序遍历一次
class Solution {
public:
int countNodes(TreeNode* root) {
int ans = 0;
pre(root, ans);
return ans;
}
void pre(TreeNode *root, int &ans){
if(root == NULL)return;
ans++;
pre(root->left, ans);
pre(root->right, ans);
}
};
解法3 考虑到完全二叉树的特点:对于每一个节点来说,总有一边是满二叉树,高度为\(d\)满二叉树共有\(2^d-1\)个节点
class Solution {
public:
int countNodes(TreeNode* root) {
int l_h = get_left_h(root);
int r_h = get_right_h(root);
if(l_h == r_h)return pow(2, l_h) - 1;
return 1 + countNodes(root->left) + countNodes(root->right);
}
int get_left_h(TreeNode *root){
int d = 0;
while(root){
d++;
root = root->left;
}
return d;
}
int get_right_h(TreeNode *root){
int d = 0;
while(root){
d++;
root = root->right;
}
return d;
}
};
解法4 考虑完全二叉树的特点:只有最后一层是不满的,采用二分查找确定是从哪里分割的,即寻找第一个没有出现在叶子节点中的节点编号
如何判断一个叶子节点是否存在
将叶子节点编号为\([0, 1, ..., 2^d -1]\),根节点将叶子节点分成了\([0, \frac{2^d-1}{2}-1]\)和\([\frac{2^d-1}{2}, 2^d-1]\)两部分,查找编号为idx的叶子时:
- idx <= mid, root = root->left
- idx > mid, root = root->right

如何查找第一个不存在的叶子结点的编号
假设第一个不存在的叶子结点在区间\([l, r]\)中,判断中点\(mid = (l + r) / 2\)
- exist(mid) == true :中点及左侧区间被排除
- exist(mid) == false : 右侧区间被排除
class Solution {
public:
int countNodes(TreeNode* root) {
int l_h = get_left_h(root);
int r_h = get_right_h(root);
if(l_h == r_h)return pow(2, r_h) - 1;
int l = 0, r = pow(2, r_h) - 1;
while(l < r){
int mid = (l + r) / 2;
if(exist(mid, r_h, root)){
l = mid + 1;
}else{
r = mid;
}
}
return pow(2, r_h) + l-1;
}
bool exist(int val, int d, TreeNode *root){
int l = 0, r = pow(2, d)-1;
TreeNode *cur = root;
for(int i = 0; i < d; ++i){
int mid = (l + r) / 2;
if(val <= mid){
r = mid;
cur = cur->left;
}else{
l = mid + 1;
cur = cur->right;
}
}
return cur != NULL;
}
int get_left_h(TreeNode *root){
int d = 0;
while(root){
d++;
root = root->left;
}
return d;
}
int get_right_h(TreeNode *root){
int d = 0;
while(root){
d++;
root = root->right;
}
return d;
}
};
【刷题-LeetCode】222. Count Complete Tree Nodes的更多相关文章
- 【刷题笔记】LeetCode 222. Count Complete Tree Nodes
题意 给一棵 complete binary tree,数数看一共有多少个结点.做题链接 直观做法:递归 var countNodes = function(root) { if(root===nul ...
- [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- 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 ...
- (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 ...
- leetcode 222.Count Complete Tree Nodes
完全二叉树是从左边开始一点点填充节点的,因此需要计算所有的节点的个数. 则分别从左边和右边来进行传递的,当左右是完全二叉树的时候,其节点个数就是pow(2,h)-1. /** * Definition ...
- [leetcode]222. Count Complete Tree Nodes完全二叉树的节点数
/* 满二叉树的特点是2^n-1,对于完全二叉树,一个node如果左右子树深度相同,那么 是一个满二叉树.如果不是,那就把node算上,继续往下看,下边的可能是满二叉树 由于完全二叉树中有一些子满二叉 ...
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
- leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
- LeetCode OJ 222. Count Complete Tree Nodes
Total Accepted: 32628 Total Submissions: 129569 Difficulty: Medium Given a complete binary tree, cou ...
随机推荐
- CF1082A Vasya and Book 题解
Content 给定 \(T\) 组数据,每组数据给出四个整数 \(n,x,y,d\).小 V 有一本 \(n\) 页的书,每次可以恰好翻 \(d\) 页,求从第 \(x\) 页恰好翻到第 \(y\) ...
- python2升级到python3 yum不可用解决方案
/usr/libexec/urlgrabber-ext-down /usr/bin/yum 这两个文件解释器 写 /usr/bin/python2
- Spring Boot定时任务配置
import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.a ...
- Java中PO、DO、DTO、 VO、 BO、POJO 、DAO、TO的概念
1. PO(persistant object) 持久对象 在 O/R 映射的时候出现的概念,如果没有 O/R 映射,没有这个概念存在了. 通常对应数据模型 ( 数据库 ), 本身还有部分业务逻辑的 ...
- Sublime Text 3结合Chrome实现网页的自动刷新
我们在编写前端代码时,写好一部分代码时想要看一看代码的实现效果,每次都要手动刷新会非常麻烦,神器来了,LiveReload插件实现网页的实时刷新,操作方法如下: 1. 官网下载Sublime Text ...
- Python基础入门(8)- Python模块和包
1.包与模块的定义与导入 1.1.什么是python的包与模块 包就是文件夹,包中还可以有包,也就是子文件夹 一个个python文件模块 1.2.包的身份证 __init__.py是每一个python ...
- IntelliJ IDEA 2019.3 代码提示忽略大小写(IDEA 2019版本如何设置代码提示不分大小写?)
最近在使用IDEA,发现每次只能进行完全匹配,且区分大小写,界面变了IDEA 2019.3 忽略大小写设置跟之前的版本稍微有点不同,跟之前的软件有点点区别,在此记录一下不区分大小写的方法. 1. 使用 ...
- Java程序设计基础笔记 • 【第2章 变量与数据类型】
全部章节 >>>> 本章目录 2.1 变量 2.1.1 变量的概念 2.1.2 变量的使用 2.1.3 实践练习 2.2 数据类型 2.2.1 数据类型的种类 2.2.2 ...
- 移动端的样式重置(CSS RESET)
/********** * reset *********/ * {box-sizing: border-box; -webkit-tap-highlight-color: rgba(0,0,0,0) ...
- Browser Events 常用浏览器事件
事件 说明 click 鼠标点击时触发此事件 dblclick 鼠标双击时触发此事件 mousedown 按下鼠标时触发此事件 mouseup 鼠标按下后松开鼠标时触发此事件 mouseover 当鼠 ...