二叉树 判断叶子节点的数目以及交换左右子树

【问题描述】二叉树按照二叉链表的方式存储。编写程序,计算二叉树中叶子结点的数目并输出;编写程序,将二叉树的左右子树进行交换,并输出交换后的二叉树的后序遍历序列。
【输入形式】二叉树的前序遍历序列,空指针的位置输入字符#
【输出形式】叶子结点的数目 左右子树交换后,后序遍历的序列,空子树的位置输出字符#
【样例输入】ABE##F##CG###
【样例输出】
3
###GC##F##EBA

 

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 #define N 100
  4 typedef struct Tree{
  5 
  6     char data;
  7     struct Tree *LeftChild;
  8     struct Tree *RightChild;
  9 }BiTNode,*BiTree;
 10 int leafcount=0; //这个计算叶子节点的个数 要是全局变量 因为在函数传递时压根不传递int参数无法传递地址(引用传递不行)所以只能全局变量
 11 BiTree CreateTree(){
 12 //前序创建一颗二叉树
 13     BiTree tree;
 14     char ch;
 15     cin>>ch;
 16     if(ch=='#'){ return tree=NULL;}
 17     else{
 18         tree=(BiTree)malloc(sizeof(BiTNode));
 19         tree->data=ch;
 20         tree->LeftChild=CreateTree();
 21         tree->RightChild=CreateTree();
 22         return tree;
 23     }
 24 
 25 }
 26 void PostOrder(BiTree root){
 27       if(root){
 28          PostOrder(root->LeftChild);
 29          PostOrder(root->RightChild);
 30          cout<<root->data;
 31       }
 32       else{
 33         cout<<'#';
 34       }
 35 
 36 }
 37 /*void PostOrder(BiTree root){ //后续非递归遍历
 38 
 39       BiTree p,q;
 40       int top=-1;
 41       BiTree *s;
 42       q=NULL;
 43       s=(BiTree*)malloc((N)*sizeof(BiTree));//创建栈
 44       while(root!=NULL||top!=-1){//节点不为空or栈不为空
 45            //if(root==NULL){cout<<"#";}节点为空时要输出# 但是不知道放在哪个位置
 46 
 47            if(root){
 48              s[++top]=root;
 49              root=root->LeftChild;
 50            }
 51            else {
 52               root=s[top];//取出栈顶元素
 53               if(root->RightChild==NULL||root->RightChild==q){
 54                    cout<<root->data;
 55                    q=root;
 56                    top--;
 57                    root=NULL;
 58               }
 59               else{
 60                 root=root->RightChild;
 61               }
 62            }
 63 
 64       }
 65 
 66 
 67 }*/
 68 int LeafCount(BiTree root){ //输出叶子节点的个数
 69 
 70         if(root){
 71             if(root->LeftChild==NULL&&root->RightChild==NULL){
 72                  leafcount++;
 73             }
 74             LeafCount(root->LeftChild);
 75             LeafCount(root->RightChild);//这算前序遍历计算 三种遍历方法都可以的
 76         }
 77        return leafcount;
 78 }
 79 BiTree Exchange(BiTree root){//交换左右子树 也是递归的算法  前序后序皆可 换换语句顺序就好 中序左中右麻烦一点
 80        BiTree temp;
 81        if(root){
 82            root->LeftChild=Exchange(root->LeftChild);
 83            root->RightChild=Exchange(root->RightChild);
84 temp=root->LeftChild; 85 root->LeftChild=root->RightChild; 86 root->RightChild=temp; 87 88 } 89 90 91 } 92 int main(){ 93 94 BiTree pt; 95 pt=CreateTree();//创建一颗二叉树 96 cout<<LeafCount(pt)<<endl; 97 pt=Exchange(pt); 98 PostOrder(pt); 99 100 }

 

转载于:https://www.cnblogs.com/yundong333/p/11009424.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值