leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
完全二叉树的定义:若设二叉树的深度为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的更多相关文章
- LeetCode 958. Check Completeness of a Binary Tree
原题链接在这里:https://leetcode.com/problems/check-completeness-of-a-binary-tree/ 题目: Given a binary tree, ...
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
- 【刷题-LeetCode】222. Count Complete Tree Nodes
Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. Note: Definition ...
- 【LeetCode】958. Check Completeness of a Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- 【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 ...
- 958. Check Completeness of a Binary Tree
题目来源 题目来源 C++代码实现 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...
- 【刷题笔记】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 ...
随机推荐
- 如何简单的在 ASP.NET Core 中集成 JWT 认证?
前情提要:ASP.NET Core 使用 JWT 搭建分布式无状态身份验证系统 文章超长预警(1万字以上),不想看全部实现过程的同学可以直接跳转到末尾查看成果或者一键安装相关的 nuget 包 自上一 ...
- 配置hadoop-eclipse-plugin(版本hadoop2.7.3):
配置hadoop-eclipse-plugin(版本hadoop2.7.3): 1:首先下载我们需要的 hadoop-eclipse-plugin-2.7.3.jar,winutils.exe 和 ...
- 网页布局设计css中单位px和em,rem的区别
国内的设计师大都喜欢用px,而国外的网站大都喜欢用em和rem,那么三者有什么区别,又各自有什么优劣呢? PX特点 1. IE无法调整那些使用px作为单位的字体大小: 2. 国外的大部分网站能够调整的 ...
- CSS格式化排版--排版
1.文字排版--字体:利用font-family设置字体,注意设置的字体必须是本地电脑中存在的字体. 例子:class="MicrosoftYahei"的h1标签的字体设置为 宋体 ...
- python之锁, 队列
进程的其他方法 进程id,进程名字,查看进程是否活着is_alive() terminate()发送结束进程的信号 import time import os from multiprocessin ...
- 用animate改变了某个样式之后再用css控制会无效
我前几天写了个移动端运动,(这个运动的代码我贴在这了:http://www.cnblogs.com/weiman/p/6600380.html)然后发现了这个问题,具体如下: 未运动前是这样的,菜单栏 ...
- 我的第一个个人博客网站 -> wizzie.top
从去年下半年实习结束,到找到第一个属于自己的工作,我就开始着手搭建自己的网站. 使用阿里云学生服务器,域名,备案解析后,开始设计网站结构和页面布局. 因为临近毕业,网站真的是写的页面怎么多怎么写,所以 ...
- matlab练习程序(Levenberg-Marquardt法最优化)
上一篇博客中介绍的高斯牛顿算法可能会有J'*J为奇异矩阵的情况,这时高斯牛顿法稳定性较差,可能导致算法不收敛.比如当系数都为7或更大的时候,算法无法给出正确的结果. Levenberg-Marquar ...
- 【js基础】创建对象的几种常见模式(工厂模式,构造函数模式,原型模式,构造原型组合模式)
一.工厂模式 缺点:没有解决对象识别的问题 优点:解决了创建多个相似对象的问题 function createPerson(name,age,job){ var o = new Object(); o ...
- Python多继承
# -*- coding: utf-8 -*- """ Created on Tue Nov 13 16:56:03 2018 @author: zhen "& ...