当前位置:网站首页>Judge whether a tree is a complete binary tree
Judge whether a tree is a complete binary tree
2022-06-21 06:09:00 【Move forward_ On my way】
problem :
There is a binary tree , Given its root node root, Please design an algorithm to judge whether it is a complete binary tree
analysis :
Because it is necessary to judge whether a tree is a complete binary tree , Consider using BFS Algorithm , Judge layer by layer :
1、 If a node has a right subtree but no left subtree , Just go back to false
2、 If a node has no subtree , The next node is not a leaf node , return false
Code :
bool check(TreeNode* root){
bool leaf = false;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int sz = q.size();
for(int i=0; i<sz; ++i){
TreeNode* curr = q.front(); q.pop();
TreeNode* l = curr->left;
TreeNode* r = curr->right;
if(r && !l || leaf&&l || leaf&&r) return false;
if(l) q.push(l);
if(r) q.push(r);
else leaf = true;
}
}
return true;
}边栏推荐
猜你喜欢

【JVM】 类加载器(ClassLoader)

BGP - route announcement and reflector (explanation + configuration command)

二叉排序树的基本操作

After the code is generated by the code generator, the copy is completed, and the module is not displayed on the web page

FPGA - 7系列 FPGA SelectIO -06- 逻辑资源之ODELAY

Error running code generator file: the server time zone value ' й ��� ʱ ��‘ is unrecognized or represents more than one time
![[MySQL] SQL statement execution process of MySQL](/img/c8/76726de7ae3521f709e336a60ae3a2.png)
[MySQL] SQL statement execution process of MySQL

深度学习的几种优化方法

成立时间用了时间插件,但在网页上修改参数的时候,如果未修改时间第一次保存会报错,第二次才行

FPGA - 7系列 FPGA SelectIO -03- 逻辑资源之ILOGIC
随机推荐
IDEA 使用记录
FPGA - 7系列 FPGA SelectIO -01- 简介与DCI技术简介
The time plug-in is used for the establishment time, but when modifying parameters on the web page, if the time is not modified, an error will be reported when saving it for the first time, and it can
FPGA - 7系列 FPGA SelectIO -05- 逻辑资源之OLOGIC
构建和保护小型网络考试
[JVM] classloader
Broadcast mechanism of numpy
xshell7远程连接服务器,挂起进程一直维持程序的运行
【MYSQL】MySQL的SQL语句执行流程
tf. Operation
lambda-stream
C language course design (detailed explanation of clothing management system)
内卷大厂系列《LRU 缓存淘汰算法》
图着色问题回溯法(最通俗易懂)
Discussion on the American ess audio DAC decoder chip es9023
牛客-TOP101-BM25
模块 14 - 15:网络应用通信考试
成立时间用了时间插件,但在网页上修改参数的时候,如果未修改时间第一次保存会报错,第二次才行
pyshark使用教程
DP背包总结