完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。

解题思路:将树按照层进行遍历,如果出现null后还出现非null则能证明这个不是完全二叉树

https://leetcode.com/problems/check-completeness-of-a-binary-tree/discuss/205768/Java-easy-Level-Order-Traversal-one-while-loop

class Solution {
public:
bool isCompleteTree(TreeNode* root) {
bool end = false;
if(root == NULL)
return true;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
TreeNode* node = q.front();
q.pop();
if(node == NULL)
end = true;
else{
if(end)
return false;
q.push(node->left);
q.push(node->right);
}
}
return true;
}
};

222. Count Complete Tree Nodes

这题是计算完全二叉树有多少个节点,按照层序遍历,找到第一个为空的节点就停止计算就好了。

注意:将node = q.front()放在循环的末尾,不放在一开始。因为每一次while都需要判断node是否为空,如果放在开始,后面的push操作就会写很多冗余的代码。同时,放在后面的话,也要在循环

外就进行初始化

  

class Solution {
public:
int countNodes(TreeNode* root) {
int count = ;
queue<TreeNode*> q;
q.push(root);
TreeNode* node = root;
while(node != NULL){
q.pop();
count++;
q.push(node->left);
q.push(node->right);
node = q.front();
}
return count;
}
};

另一种写法,自己的:

/**
* 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 ;
queue<TreeNode*> q;
q.push(root);
int count = ;
while(!q.empty()){
TreeNode* tmp = q.top();
if(!tmp)
break;
count++;
q.pop();
q.push(tmp->left);
q.push(tmp->right);
}
return count;
}
};

leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes的更多相关文章

  1. LeetCode 958. Check Completeness of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/check-completeness-of-a-binary-tree/ 题目: Given a binary tree, ...

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

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

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

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

  4. 【LeetCode】958. Check Completeness of a Binary Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...

  5. 【leetcode】958. Check Completeness of a Binary Tree

    题目如下: Given a binary tree, determine if it is a complete binary tree. Definition of a complete binar ...

  6. 958. Check Completeness of a Binary Tree

    题目来源 题目来源 C++代码实现 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...

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

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

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

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

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

随机推荐

  1. Java字符串String

    Java字符串String 我们知道Java的字符窜是Immutable(不可变)的,一旦创建就不能更改其内容了:平常我们对字符串的操作是最多的,其实对字符串的操作,返回的字符串都是新建的字符串对象, ...

  2. MYSQL中SHOW的使用整理收藏

    好记性不如乱笔头吧....下面收藏整理了mysql中show 的使用技巧....有需要的博友可以看看哈 a. show tables或show tables from database_name; / ...

  3. SQLite 的 CodeFirst 模式

    目录 问题描述 解决方案 安装依赖包 修改程序配置 App.config 创建模型对象 Person.cs 创建数据上下文 PersonDbContext.cs 主程序调用 Program.cs 注意 ...

  4. javascript之揭示模式

    一.该模式优缺点1.优点:该模式可以使脚本语法更加一致,在模块代码底部,它很容易指出哪些函数和变量可以被公开访问,从而改善可读性. 2.缺点:如果一个私有函数引用一个公有函数,公有函数是不能被覆盖的. ...

  5. spark任务提交流程

    这个是我在网上搬的: 原博客地址为:https://blog.csdn.net/xwc35047/article/details/78732738 上图是client以spark-submit形式提交 ...

  6. 创建一个背景为蓝色的pygame窗口

    import sys import pygame def creat_screen(): #初始化pygame pygame.init() #设置窗口大小并保存在screen对象中 screen = ...

  7. Python 基于urllib.request封装http协议类

    基于urllib.request封装http协议类 by:授客QQ:1033553122 测试环境: Python版本:Python 3.3   代码实践 #!/usr/bin/env python ...

  8. ImageButton和ImageView设置点击透明区域不响应

    思路 ImageView和ImageButton都可以设置background和设置src,两者的区别自行度娘.由于两者的不同,获取它们的图片资源的方法也不同.倘若设置的是background,那么需 ...

  9. Django基础篇--模板和路由分发

    Django模板 首先什么是一个模板? 简单来说就是一个网页,可以被view响应给用户 目的是为了解决复杂的显示问题 2. 模板的设置问题 setting.py中的TEMPLATES配置 1)BACK ...

  10. (后端)Java新人入职——配置环境及安装开发工具(完全)

    转自csdn:执笔记忆的空白 很多新人对于进入新公司,相关工具的安装和环境变量的设定很苦恼.又苦于没有完整的配置开发环境的资料,我这里写一篇操作步骤的案例, 至少让你能把开发工具安装起来,并实用起来, ...