当前位置:网站首页>Complete Binary Tree
Complete Binary Tree
2022-07-27 05:01:00 【Small l ~~~】
Given a tree, you are supposed to tell if it is a complete binary tree.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤20) which is the total number of nodes in the tree – and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a - will be put at the position. Any pair of children are separated by a space.
Output Specification:
For each case, print in one line YES and the index of the last node if the tree is a complete binary tree, or NO and the index of the root if not. There must be exactly one space separating the word and the number.
Sample Input 1:
9
7 8
- -
- -
- -
0 1
2 3
4 5
- -
- -
Sample Output 1:
YES 8
Sample Input 2:
8
- -
4 5
0 6
- -
2 3
- 7
- -
- -
Sample Output 2:
NO 1
The main idea of the topic :
Judge whether a tree is a complete binary tree , Yes, output YES And the last node , Otherwise output NO And root nodes .
Wang daoshu also has the same algorithm problem , The idea of the algorithm is the same , Only the tree required in Wang daoshu is stored in a binary linked list storage structure .
Ideas :
Using hierarchical traversal algorithm , Add all nodes to the queue ( Including empty nodes ). When an empty node is encountered , See whether there is a non empty node after it . If you have any , Then the binary tree is not a complete binary tree .
#include<bits/stdc++.h>
using namespace std;
struct Complete_Binary_Tree
{
int data;
int lchild, rchild;
}node[25];
int n;
bool visited[25] = {
false};
int turn(string s) {
int sum = 0;
for(auto &i:s) sum = sum * 10 + (i - '0');
return sum;
}
int build_tree() {
scanf("%d", &n);
for(int i = 0; i < n; i++) {
string left, right;
cin >> left >> right;
node[i].data = i;
if(left == "-") node[i].lchild = -1;
else {
node[i].lchild = turn(left);
visited[node[i].lchild] = true;
}
if(right == "-") node[i].rchild = -1;
else {
node[i].rchild = turn(right);
visited[node[i].rchild] = true;
}
}
for(int i = 0; i < n; i++) {
if(!visited[i]) return i;// Return root node
}
}
int last_node = 0;
bool iscomplete(int root) {
queue<int>q;
if(n == 0) return true; // An empty tree is a full binary tree
q.push(root);
while (!q.empty())
{
int top = q.front();
q.pop();
if(top != -1) last_node = top;
if(top != -1) {
q.push(node[top].lchild);
q.push(node[top].rchild);
} else {
// The node is empty , Check whether there are non empty nodes after
while(!q.empty()) {
int temp = q.front();
q.pop();
if(temp != -1) return false; // Node is not empty , Then the binary tree is an incomplete binary tree
}
}
}
return true;
}
int main() {
int root = build_tree();
// for(int i = 0; i < n; i++) cout << node[i].lchild << " " << node[i].rchild << endl;
// cout << root << endl;
if(iscomplete(root)) printf("YES %d", last_node);
else printf("NO %d", root);
return 0;
}
边栏推荐
- Basic configuration of static routing to achieve network wide accessibility
- 如何将Photoshop图层复制到其他文档
- 「Photoshop2021入门教程」对齐与分布制作波点图案
- Hiding skills of Photoshop clipping tool
- 集成开发环境Pycharm的安装及模板设置
- Network protocol details: IP
- 不需手动安装cuda和cudnn,通过一行程序即可安装tensorflow-gpu,以tensorflow-gpu2.0.0,cuda10.0,cudnn7.6.5为例
- [C language] detailed explanation of user-defined types (structure + enumeration + Union)
- How to copy Photoshop layers to other documents
- STM32 Hal serial port (uart/usart) debugging experience (I) -- basic knowledge of serial port communication +hal library code understanding
猜你喜欢

Review of various historical versions of Photoshop and system requirements
![[search] flood fill and shortest path model](/img/22/5240c9ff6ea3c7c1017e3e9a4a27cb.png)
[search] flood fill and shortest path model

Interesting C language

树莓派输出PWM波驱动舵机

ps样式如何导入?Photoshop样式导入教程

Error: cannot read properties of undefined (reading 'then')

CDH cluster integration external Flink (improved version - keep pace with the times)

小程序项目如何创建

Hiding skills of Photoshop clipping tool

C语言 通讯录管理系统(链表,手机号码分段存储,txt文件存取,完整源码)
随机推荐
kali系统arp介绍(断网嗅探密码抓包)
Why is select not recommended*
Explanation of index failure principle and its common situations
Read write separation and master-slave synchronization
再一个技巧,每月稳赚3万+
STM32_ HAL_ SUMMARY_ NOTE
Visualization domain svg
HCIA dynamic routing OSPF experiment
如何做数据平滑迁移:双写方案
Hiding skills of Photoshop clipping tool
How to copy Photoshop layers to other documents
[search] connectivity model of DFS + search order
How to import PS style? Photoshop style import tutorial
[search] two way search + A*
【C语言】动态内存管理
vim的基本操作
日落红暖色调调色滤镜luts预设Sunset LUTs 1
How does PS import LUT presets? Photoshop import LUT color preset tutorial
Find a specific number in an ordered array
When using Photoshop, the prompt "script error -50 general Photoshop error appears“