当前位置:网站首页>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
边栏推荐
- In redis cluster, the master node fails, and the IP changes after the master-slave switch. The client does not need to deal with it
- Using positive and negative traversal to solve the problem of "the shortest distance of characters"
- C# 11 对 ref 和 struct 的改进
- tf. keras. layers. Conv? D function
- ASP.NET和ASP.NETCore多环境配置对比
- Laravel's own paging query
- 如果通过 C# 实现对象的深复制 ?
- Impact of AOT and single file release on program performance
- Maui initial experience: Cool
- Onenet connection process
猜你喜欢

How to write the expected salary on your resume to double your salary during the interview?

Laravel8- use JWT

tf. keras. layers. Embedding function

Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system

Openfeign details show

ASP. Net 6 middleware series - execution sequence

The whole network is the most complete. How to do interface automation test? Proficient in interface automation test details

The most detailed in the whole network, software testing measurement, how to optimize software testing cost and improve efficiency --- hot

Service avalanche effect

Some problems encountered in setting Django pure interface, channel and MySQL on the pagoda panel
随机推荐
由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
If the deep replication of objects is realized through C #?
C# WPF UI框架MahApps切换主题
对.NET未来的一点感悟
C# 读写二进制文件
C#语法糖空合并运算符【??】和空合并赋值运算符【 ??=】
編碼電機PID調試(速度環|比特置環|跟隨)
Passing object type parameters through openfeign
【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持
The most detailed in the whole network, software testing measurement, how to optimize software testing cost and improve efficiency --- hot
TP5 inherits base and uses the variables in base
How does Microsoft solve the problem of multiple programs on PC side -- internal implementation
PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
Array and collection types passed by openfeign parameters
.NET7之MiniAPI(特别篇):.NET7 Preview3
How to write the expected salary on your resume to double your salary during the interview?
Maui initial experience: Cool
Openfeign details show
Blazor University (12)组件 — 组件生命周期