当前位置:网站首页>交换二叉树中每个结点的左和右
交换二叉树中每个结点的左和右
2022-04-23 03:03:00 【学习kl&tk】
以二叉链表作为二叉树的存储结构,交换二叉树中每个结点的左孩子和右孩子。
输入格式:
输入二叉树的先序序列。
提示:一棵二叉树的先序序列是一个字符串,若字符是‘#’,表示该二叉树是空树,否则该字符是相应结点的数据元素。
输出格式:
输出有两行:
第一行是原二叉树的中序遍历序列;
第二行是交换后的二叉树的中序遍历序列。
输入样例:
ABC##DE#G##F###
输出样例:
CBEGDFA
AFDGEBC
#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;
}
string s;
void dfs(Tree* root){
if(root == NULL) return;
if(root->lchild) dfs(root->lchild);
s+=root->val;
if(root->rchild) dfs(root->rchild);
}
int main()
{
Tree* root = bulid();
dfs(root);
cout << s<< endl;
reverse(s.begin(),s.end());
cout << s<< endl;
return 0;
}
版权声明
本文为[学习kl&tk]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_53013914/article/details/124280619
边栏推荐
- tf. keras. layers. Conv? D function
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (9)
- 腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!
- ASP.NET和ASP.NETCore多环境配置对比
- Opencv reads webcam video and saves it locally
- Wepy learning record
- L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
- L2-006 树的遍历(中后序确定二叉树&层序遍历)
- 最通俗易懂的依赖注入与控制反转
- AC380V drop 5v12v24v200ma, UHV non isolated chip IC scheme
猜你喜欢

Binary tree
![[software testing] understand the basic knowledge of software testing](/img/ff/8fcd4b88de28505989aaf517d16113.png)
[software testing] understand the basic knowledge of software testing

Detailed explanation of distributed things

微软是如何解决 PC 端程序多开问题的

Source Generator实战

Liunx foundation - zabbix5 0 monitoring system installation and deployment

Huawei machine test question -- deformation of hj53 Yang Hui triangle

L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)

Distributed system services

tf. keras. layers. Density function
随机推荐
Configuring Apache Web services for servers such as Tianyi cloud
Cherno_ Game engine series tutorial (5): 101~
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
Reverse a linked list < difficulty coefficient >
Chapter VII project communication management of information system project manager summary
利用栈的回溯来解决“文件的最长绝对路径”问题
《信息系统项目管理师总结》第四章 项目成本管理
使用栈来解决”迷你语法分析器“的问题
ASP.NET 6 中间件系列 - 执行顺序
c#语法糖模式匹配【switch 表达式】
《信息系統項目管理師總結》第六章 項目人力資源管理
AC380V drop 5v12v24v200ma, UHV non isolated chip IC scheme
Niuke white moon race 5 [problem solving mathematics field]
Xamarin效果第二十一篇之GIS中可扩展浮动操作按钮
【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持
Processes and threads
Shell learning notes -- shell processing of output stream awk
Recursion - outputs continuously increasing numbers
.NET7之MiniAPI(特别篇):.NET7 Preview3
Opencv fills the rectangle with a transparent color