当前位置:网站首页>Swap the left and right of each node in a binary tree
Swap the left and right of each node in a binary tree
2022-04-23 03:07:00 【Learning KL & TK】
Binary linked list is used as the storage structure of binary tree , Swap the left child and right child of each node in the 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 original binary tree ;
The second line is the middle order traversal sequence of the binary tree after exchange .
sample input :
ABC##DE#G##F###
sample output :
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;
}
版权声明
本文为[Learning KL & TK]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230303365519.html
边栏推荐
- Deep q-network (dqn)
- AC & A2C & A3C
- 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
- Binary tree
- 由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。
- The most detailed in the whole network, software testing measurement, how to optimize software testing cost and improve efficiency --- hot
- Use DFS to solve the problem of "number of dictionary rows"
- 一套关于 内存对齐 的C#面试题,做错的人很多!
- Detailed explanation of distributed things
- Blazor University (12)组件 — 组件生命周期
猜你喜欢

由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。

Small companies don't make formal offers

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

Mise en service PID du moteur de codage (anneau de vitesse | anneau de position | suivant)

基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?

腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元

Response processing of openfeign

Maui initial experience: Cool

PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type

C#中切片语法糖的使用
随机推荐
tf. keras. layers. Density function
Laravel's own paging query
Use DFS to solve the problem of "number of dictionary rows"
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比
Use of MySQL command line client and common commands
ASP.NET 6 中间件系列 - 条件中间件
先中二叉建树
Use of metagroup object tuple in C
数据挖掘系列(3)_Excel的数据挖掘插件_估计分析
Some problems encountered in setting Django pure interface, channel and MySQL on the pagoda panel
Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
C# 11 的这个新特性,我愿称之最强!
宁德时代地位不保?
Laravel new route file
TP5 customization in extend directory succeeded and failed. Return information
Recursion - outputs continuously increasing numbers
Creating wechat voucher process with PHP
MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
Detailed explanation of distributed things