当前位置:网站首页>求二叉树的叶子结点个数
求二叉树的叶子结点个数
2022-04-23 03:03:00 【学习kl&tk】
以二叉链表作为二叉树的存储结构,求二叉树的叶子结点个数。
输入格式:
输入二叉树的先序序列。
提示:一棵二叉树的先序序列是一个字符串,若字符是‘#’,表示该二叉树是空树,否则该字符是相应结点的数据元素。
输出格式:
输出有两行:
第一行是二叉树的中序遍历序列;
第二行是二叉树的叶子结点个数。
输入样例:
ABC##DE#G##F###
输出样例:
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;
}
版权声明
本文为[学习kl&tk]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_53013914/article/details/124278251
边栏推荐
- Use of MySQL command line client and common commands
- TP5 multi conditional where query (using PHP variables)
- TP5 inherits base and uses the variables in base
- HLS / chisel practice CORDIC high performance computing complex square root
- Openfeign timeout setting
- How to write the expected salary on your resume to double your salary during the interview?
- JS relearning
- How to count the number of all files in a directory under win10 system
- JS learning notes
- Chapter VII project communication management of information system project manager summary
猜你喜欢

Encapsulation of ele table

樹莓派開發筆記(十二):入手研華ADVANTECH工控樹莓派UNO-220套件(一):介紹和運行系統

Onenet connection process

Q-Learning & Sarsa

Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)

全网最全,接口自动化测试怎么做的?精通接口自动化测试详解

Plug in for vscode

Array and collection types passed by openfeign parameters

AspNetCore配置多环境log4net配置文件
![How to use C language to realize [guessing numbers game]](/img/8c/052dcb0ce64ee1713bebb1340248e6.png)
How to use C language to realize [guessing numbers game]
随机推荐
It turns out that PID was born in the struggle between Lao wangtou and Lao sky
Guangcheng cloud service can fill in a daily report regularly every day
Vs code setting line feed
Blazor University (11)组件 — 替换子组件的属性
Traversal of l2-006 tree (middle and later order determination binary tree & sequence traversal)
Shell script learning -- practical case
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
Redis data server / database / cache (2022)
TP5 where query one field is not equal to multiple values
Basic SQL (VIII) data update operation practice
Cherno_ Game engine series tutorial (5): 101~
Sonic cloud real machine tutorial
【鉴权/授权】自定义一个身份认证Handler
一套关于 内存对齐 的C#面试题,做错的人很多!
Some problems encountered in setting Django pure interface, channel and MySQL on the pagoda panel
微软是如何解决 PC 端程序多开问题的
JS learning notes
Typescript Learning Guide
Depth deterministic strategy gradient (ddpg)
Deep q-network (dqn)