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


    【问题描述】二叉树按照二叉链表的方式存储。编写程序,计算二叉树中叶子结点的数目并输出;编写程序,将二叉树的左右子树进行交换,并输出交换后的二叉树的后序遍历序列。
    【输入形式】二叉树的前序遍历序列,空指针的位置输入字符#
    【输出形式】叶子结点的数目 左右子树交换后,后序遍历的序列,空子树的位置输出字符#
    【样例输入】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 }
  • 相关阅读:
    @ResponseBody和@RequestBoby的作用
    项目Alpha冲刺Day9
    项目Alpha冲刺Day8
    项目Alpha冲刺Day7
    设计模式第三次作业
    项目Alpha冲刺Day6
    树莓派历程笔记
    项目Alpha冲刺Day5
    django获取ip与数据重复性判定
    在django模板中添加jquery
  • 原文地址:https://www.cnblogs.com/yundong333/p/11009424.html
Copyright © 2020-2023  润新知