当前位置:网站首页>求二叉树的叶子结点个数
求二叉树的叶子结点个数
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
边栏推荐
猜你喜欢
HLS / chisel practice CORDIC high performance computing complex square root
利用栈的回溯来解决“文件的最长绝对路径”问题
Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
[software testing] understand the basic knowledge of software testing
.NET点滴:说说Middleware构造中获取不到Scoped服务的问题
Niuke white moon race 6 [solution]
全网最全,接口自动化测试怎么做的?精通接口自动化测试详解
Tips in MATLAB
Onenet connection process
Source Generator实战
随机推荐
AC380V drop 5v12v24v200ma, UHV non isolated chip IC scheme
ASP.NET 6 中间件系列 - 自定义中间件类
Vs code setting line feed
Plug in for vscode
tf. keras. layers. Timedistributed function
Opencv combines multiple pictures into video
JS relearning
Depth deterministic strategy gradient (ddpg)
Numpy append function
FileNotFoundError: [Errno 2] No such file or directory
LNMP MySQL allows remote access
How to deploy a website with only a server and no domain name?
Sonic cloud real machine tutorial
【鉴权/授权】自定义一个身份认证Handler
The space between the left and right of the movie ticket seats is empty and cannot be selected
Classification of technology selection (2022)
C# 11 对 ref 和 struct 的改进
利用正反遍历来解决“字符的最短距离”问题
SQL statement - DDL
Systemctl start Prometheus + grafana environment