1. 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的更多相关文章

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

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

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

  5. leetcode 222.Count Complete Tree Nodes

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

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

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

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

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

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

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

  9. LeetCode OJ 222. Count Complete Tree Nodes

    Total Accepted: 32628 Total Submissions: 129569 Difficulty: Medium Given a complete binary tree, cou ...

随机推荐

  1. Asp.Net Core基础篇之:白话管道中间件

    在Asp.Net Core中,管道往往伴随着请求一起出现.客户端发起Http请求,服务端去响应这个请求,之间的过程都在管道内进行. 举一个生活中比较常见的例子:旅游景区. 我们都知道,有些景区大门离景 ...

  2. java 图形化小工具Abstract Window Toolit ;布局管理器FlowLayout流式布局;BorderLayout边界布局;GridLayout网格布局;CardLayou重叠卡片布局;BoxLayout方框布局;绝对定位

    1.FlowLayout流式布局管理器: FlowLayout布局管理器中,组件像水流一样向某方向流动(排列),遇到障碍(边界)就折回,重头开始排列 .在默认情况下,FlowLayout局管理器从左向 ...

  3. Tornado WEB服务器框架 Epoll-- 【Mysql数据库】

    5.1 数据库 与Django框架相比,Tornado没有自带ORM,对于数据库需要自己去适配.我们使用MySQL数据库. 在Tornado3.0版本以前提供tornado.database模块用来操 ...

  4. 在mysql5.8中用json_extract函数解析json

    背景:某个字段的数据中是JSON,需要提取其中的卡号部分,如: {"objType":"WARE","orderId":6771254073 ...

  5. 『与善仁』Appium基础 — 29、获取toast信息

    目录 1.toast介绍 2.toast定位 3.示例 4.封装toast判断 1.toast介绍 Android中的toast是一种简易的消息提示框,toast提示框不能被用户点击,会根据所设置的显 ...

  6. c++设计模式概述之适配器

    类写的不规范(应该屏蔽类的拷贝构造函数和运算符=).少写点代码,缩短篇幅,重在理解. 实际中可不要这样做. 类比生活中的手机,pad等电源适配器. 简单来讲: 将原本  不匹配  的两者  变的匹配  ...

  7. 【九度OJ】题目1078:二叉树遍历 解题报告

    [九度OJ]题目1078:二叉树遍历 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1078 题目描述: 二叉树的前序.中序.后序遍历 ...

  8. 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...

  9. InnoDB 聚集索引和非聚集索引、覆盖索引、回表、索引下推简述

    关于InnoDB 存储引擎的有聚集索引和非聚集索引,覆盖索引,回表,索引下推等概念,这些知识点比较多,也比较零碎,但是概念都是基于索引建立的,本文从索引查找数据讲述上述概念. 聚集索引和非聚集索引 在 ...

  10. Linux环境下Django App部署到XAMPP上

    Django App部署到XAMPP上 准备工作 首先一定要保证自己的代码在本地可以运行! 同时在服务器上把需要的库,什么数据库之类的都装好! 源码安装mod_wsgi 从mod_wsgi的gitgu ...