当前位置:网站首页>Find the number of leaf nodes of binary tree
Find the number of leaf nodes of binary tree
2022-04-23 03:07:00 【Learning KL & TK】
Binary linked list is used as the storage structure of binary tree , Find the number of leaf nodes of binary tree .
Input format :
Enter the sequence of the binary tree .
Tips : The preorder of a binary tree is a string , If the character is ‘#’, Indicates that the binary tree is empty , Otherwise, the character is the data element of the corresponding node .
Output format :
There are two lines of output :
The first line is the middle order traversal sequence of the binary tree ;
The second row is the number of leaf nodes of the binary tree .
sample input :
ABC##DE#G##F###
sample output :
CBEGDFA
3
#include "bits/stdc++.h"
using namespace std;
struct Tree{
char val;
Tree* lchild;
Tree* rchild;
};
int len = 0;
Tree* bulid(){
char c;
cin >> c;
if(c=='#') return NULL;
Tree* root = new Tree();
root->val = c;
root->lchild = bulid();
root->rchild = bulid();
return root;
}
void dfs(Tree* root){
if(root == NULL) return;
if(root->lchild == NULL && root->rchild == NULL){
len++;
}
if(root->lchild) dfs(root->lchild);
cout << root->val;
if(root->rchild) dfs(root->rchild);
}
int main()
{
Tree* root = bulid();
dfs(root);
cout << endl<<len<<endl;
return 0;
}
版权声明
本文为[Learning KL & TK]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230303365550.html
边栏推荐
- Tips in MATLAB
- 編碼電機PID調試(速度環|比特置環|跟隨)
- HLS / chisel practice CORDIC high performance computing complex square root
- AC & A2C & A3C
- Openfeign service call
- Simple example of using redis in PHP
- C语言实现通讯录----(静态版本)
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (7)
- PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
- 如果通过 C# 实现对象的深复制 ?
猜你喜欢
Ningde's position in the times is not guaranteed?
利用栈的回溯来解决“文件的最长绝对路径”问题
C# 11 的这个新特性,我愿称之最强!
Onenet connection process
[format] simple output (2)
.NET点滴:说说Middleware构造中获取不到Scoped服务的问题
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
Notes sur le développement de la tarte aux framboises (XII): commencer à étudier la suite UNO - 220 de la tarte aux framboises de contrôle industriel advantech (i): Introduction et fonctionnement du s
Small companies don't make formal offers
数据挖掘系列(3)_Excel的数据挖掘插件_估计分析
随机推荐
7-11 重排链表 (25 分)
Distributed system services
Blazor University (12)组件 — 组件生命周期
Array and collection types passed by openfeign parameters
利用栈的回溯来解决“文件的最长绝对路径”问题
Niuke white moon race 6 [solution]
TP5 where query one field is not equal to multiple values
Source generator actual combat
Use split to solve the "most common words" problem
树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统
C# WPF UI框架MahApps切换主题
【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持
一套关于 内存对齐 的C#面试题,做错的人很多!
使用两种方法来解决“最大回文数乘积”问题
利用正反遍历来解决“字符的最短距离”问题
Laravel new route file
准备一个月去参加ACM,是一种什么体验?
Miniapi of. Net7 (special section): NET7 Preview3
FileNotFoundError: [Errno 2] No such file or directory
搭建XAMPP时mysql端口被占用