完全二叉树的定义:若设二叉树的深度为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.常见方法分为迭代和递归,迭代是从头到尾,递归是从尾到头2.设置两个指针,old和new,每一项添加在new的后面,新链表头指针指向新的链表头3.old->next不能直接指向new,而是应该 ...

  2. Hash Table (youtube)

    here is a link for youtube about hash table which is super good https://www.youtube.com/watch?v=h2d9 ...

  3. unity相机跟随Player常用方式

    固定跟随,无效果(意义不大) public class FollowPlayer : MonoBehaviour { public Transform Player; private Vector3 ...

  4. Git应用—05Git文件冲突合并的几种情况(转载)

    git文件冲突合并的几种情况 https://xieye.iteye.com/blog/2433229 本文描述了git冲突的几种常见情况和解决方案,老鸟请直接忽略本文.假设冲突文件是 test/Te ...

  5. JavaScript按日期排序之灵活排序

    上代码: var dataContent = [ { ID: "1", hobbit: "去音乐", sport: "在篮球", movie ...

  6. 【Java入门提高篇】Day22 Java容器类详解(五)HashMap源码分析(上)

    准备了很长时间,终于理清了思路,鼓起勇气,开始介绍本篇的主角——HashMap.说实话,这家伙能说的内容太多了,要是像前面ArrayList那样翻译一下源码,稍微说说重点,肯定会让很多人摸不着头脑,不 ...

  7. 9.4、__del__、__doc__、__dict__、__module__、__getitem__、__setitem__、__delitem__、__str__、__repr__、__call__

    相关内容: __del__.__doc__.__dict__.__module__.__getitem__.__setitem__.__delitem__.__str__.__repr__.__cal ...

  8. jmeter利用自身代理录制脚本

    在利用代理录制脚本时一定要安装java jdk,不然不能录制的. 没有安装过java jdk安装jmeter后打开时会提示安装jdk,但是mac系统中直接打开提示安装jdk页面后下载的java并不是j ...

  9. python第六十天-----RabbitMQ

    RabbitMQ消息队列:默认为消息轮循模式,按client端启动是顺序接收 server端 import pika connection = pika.BlockingConnection(pika ...

  10. CentOS 7下systemd是如何stop mysql服务的

      [背景] 有同事在研究mongo的服务启动方式,讨论到mysql5.7的服务管理时一起做了下面测试. MySQL5.7是用systemd来管理service的,它的配置文件/usr/lib/sys ...