C语言二叉树递归算法怎么做?

Python017

C语言二叉树递归算法怎么做?,第1张

#include <stdio.h>

#include <string.h>

struct treenode{

    int value

    treenode* left

    treenode* right

}

typedef treenode* BiTree

void visit(treenode* node)

{

    printf("%2d ", node->value)

}

//    结点总数 

int node(BiTree T)

{

    if( !T ){

        return 0

    }

    return node(T->left) + node(T->right) + 1

}

//    前序 

void preOrder(BiTree T)

{

    if( T ){

        visit(T)

        preOrder(T->left)

        preOrder(T->right)    

    }

}

//    中序

void inOrder(BiTree T)

{

    if( T ){

        inOrder(T->left)

        visit(T)

        inOrder(T->right)    

    }    

//    后序

void postOrder(BiTree T)

{

    if( T ){

        postOrder(T->left)

        postOrder(T->right)    

        visit(T)

    }    

//    叶子节点数

int leafnode(BiTree T)

{

    if( T ){

        if( !T->left && !T->right )

            return 1

        else

            leafnode(T->left) + leafnode(T->right)    

    }else{

        return 0

    }

int height(BiTree T)

{

    if( T ){

        int lh = height(T->left)

        int rh = height(T->right)

        return (lh>rh ? lh:rh) + 1

    }else{

        return 0

    }

}

int main()

{

    

    return 0

}

你的说法是有问题的。

通常说,通过指针作为函数的参数,可以再被调函数中,修改实际参数所指向的变量的值,也就是把改变传递给主调函数。

但是,请看清楚上面的说法,“通过指针作为函数的参数,可以再被调函数中,修改实际参数所指向的变量的值”。是修改实际参数所指向的变量的值,而不是修改实际参数本身。

你的代码里面是要修改实际参数本身,而不是修改实际参数所指向变量。

要完成这个功能,你就需要指向指针的指针了

递归创建二叉树的输入是有讲究的,可参考:网页链接中最后的输入示例:如果你用#作为结束,则对应输入:1 2 4 # 6 ###3 #5 #7 #8 ##

再给个递归创建二叉树的例子:

#include <stdio.h>

#include <stdlib.h>

typedef struct Tree {

    int Val

    struct Tree* left

    struct Tree* right

}Tree

Tree * CreateBiTree(void)

{

    Tree * T

    int val

    scanf("%d", &val)

    if(val == 0)

        T = NULL

    else

    {

        T = (Tree *)malloc(sizeof(Tree))

        T -> Val = val

        T -> left = CreateBiTree()

        T -> right = CreateBiTree()

    }

    return T

}

void Print(Tree* root)

{

    if (root != NULL)

    {

        Print(root->left)

        printf("%d ", root->Val)

        Print(root->right)

    }

}

int main()

{

    Tree* root = CreateBiTree()

    Print(root)

    return 0

}

以上面的输入例子1 2 4 # 6 ###3 #5 #7 #8 ##为例,对应的输入为:1 2 3 0 6 0 0 0 3 0 5 0 7 0 8 0 0

运行结果:

当然也可以这样:

#include <stdio.h>

#include <stdlib.h>

typedef struct Tree {

    int Val

    struct Tree* left

    struct Tree* right

}Tree

void CreateBiTree(Tree**T)

{

    int val

    scanf("%d", &val)

    if(val == 0)

        *T = NULL

    else

    {

        *T = (Tree *)malloc(sizeof(Tree))

        (*T)->Val = val

        CreateBiTree(&(*T)->left)

        CreateBiTree(&(*T)->right)

    }

}

void Print(Tree* root)

{

    if (root != NULL)

    {

        Print(root->left)

        printf("%d ", root->Val)

        Print(root->right)

    }

}

int main()

{

    Tree* root

    CreateBiTree(&root)

    Print(root)

    return 0

}

这里的输入示例为:1 2 4 0 6 0 0 0 7 0 0 0

运行结果:

C++ 版:

#include <iostream>

using namespace std

typedef struct Tree {

    int Val

    struct Tree* left

    struct Tree* right

}Tree

void CreateBiTree(Tree* & T)

{

    int val

    cin >> val

    if(val == 0)

        T = NULL

    else

    {

        T = new Tree

        T->Val = val

        CreateBiTree(T->left)

        CreateBiTree(T->right)

    }

}

void Print(Tree*root)

{

    if (root != NULL)

    {

        Print(root->left)

        cout << root->Val << ' '

        Print(root->right)

    }

}

int main()

{

    Tree* root

    CreateBiTree(root)

    Print(root)

    return 0

}

看了一下你的递归部分的代码,是正确的,没问题。