当前位置:   article > 正文

BJFUOJ:基于二叉链表的二叉树左右节点的交换_基于二叉链表的二叉树左右孩子的交换

基于二叉链表的二叉树左右孩子的交换

BJFUOJ:基于二叉链表的二叉树左右孩子的交换

#include<bits/stdc++.h>
using namespace std;
typedef struct BiTnode{
	char data;
	struct BiTnode *left,*right;
}BiTnode,*BiTree;

void CreatTree(BiTree &t,char s[],int &i){
	if(s[i] == '0'){
		t = NULL;
	}else{
		t = new BiTnode;
		t->data = s[i];
		CreatTree(t->left,s,++i);
		CreatTree(t->right,s,++i);
	}
}

void ChangeLeftAndRight(BiTree &t){
	if(t){
		BiTree tt;
		tt = t->left;
		t->left = t->right;
		t->right = tt;
		ChangeLeftAndRight(t->left);
		ChangeLeftAndRight(t->right);
	}
}

void PreOrder(BiTree &t){
	if(t){
		cout<<t->data;
		PreOrder(t->left);
		PreOrder(t->right);
	}
}

int main(){
	while(1){
		char s[100];
		int i = 0;
		cin>>s;
		if(s[0] == '0'){
			break;
		}
		BiTree t;
		CreatTree(t,s,i);
		ChangeLeftAndRight(t);
		PreOrder(t);
		cout<<endl;
	}
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/737616
推荐阅读
相关标签
  

闽ICP备14008679号