完全二叉树的定义:若设二叉树的深度为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. [PHP] 数据结构-线性表的顺序存储结构PHP实现

    1.PHP中的数组实际上是有序映射,可以当成数组,列表,散列表,字典,集合,栈,队列,不是固定的长度2.数组定义中多个单元都使用了同一个键名,则只使用了最后一个,之前的都被覆盖了3.想要函数的一个参数 ...

  2. 安装Java语言的jdk,配置java环境变量

    一.windows 安装jdk win7 下载jdk: 地址   https://www.oracle.com/technetwork/java/javase/downloads/index.html ...

  3. 【Mybatis】一对多实例

    ①创建数据库和表,数据库为mytest,表为teacher和student DROP TABLE IF EXISTS teacher; DROP TABLE IF EXISTS student; CR ...

  4. API输出的时候是return还是echo?

    写php API写的很少,最近才开始接口的写法,在框架里面一直用return,但是在api中retrun就失效了,为什么呢? 网友给出的答案: 1. return 一般用于函数或方法的返回. echo ...

  5. vue路由传参的三种基本方式

    现有如下场景,点击父组件的li元素跳转到子组件中,并携带参数,便于子组件获取数据. 父组件中: <li v-for="article in articles" @click= ...

  6. 2018-01-19 Xtext试用: 5步实现一个(中文)JVM语言

    续上文Xtext试用: 快速实现简单领域专用语言(DSL). 基于官方教程: Five simple steps to your JVM language 达成如下语言: 它被Quan6JvmMode ...

  7. 【工具相关】Web--nodejs的安装

    一,从官网下载nodejs.org. https://nodejs.org/en/ 二,按照步骤一步一步安装就好.

  8. 关于jQuery出现的新添加元素点击事件无效

    //通常点击写法: $(".div").on('click', function () { var $this = $(this); var isActive = $this.ha ...

  9. Android-滑动解锁高亮文字自定义TextView

    public class HightLightTextView extends TextView { // 存储view的宽度 private int mTextViewWidth = 0; // 画 ...

  10. gitlab-ci的注意点

    在使用gitlab搭配gitlab-runner进行ci配置时,发现两个问题,略记如下备忘: 1. 若发现ci job控制台显示无法下载该项目,但当前提交代码人和ci用户都确实具备该项目的访问权限时, ...