面试题 二叉树的前序遍历,中序遍历,后序遍历(递归实现)

前序遍历递归解法:
(1)如果二叉树为空,空操作

(2)如果二叉树不为空,访问根节点,前序遍历左子树,前序遍历右子树

中序遍历递归解法
(1)如果二叉树为空,空操作。
(2)如果二叉树不为空,中序遍历左子树,访问根节点,中序遍历右子树

后序遍历递归解法
(1)如果二叉树为空,空操作
(2)如果二叉树不为空,后序遍历左子树,后序遍历右子树,访问根节点

参考实现代码:

[/u1/yyang/study/algorithm/binarytree](203)yyang@dcmvrh12#cat binarytree.h
#include <stdio.h>
#include <stdlib.h>

typedef struct  BinaryTreeNode
{
        int value;
        BinaryTreeNode* left;
        BinaryTreeNode* right;

}BTN;

BTN* CreateBTNode(int value)
{
        BTN* btnode = new BinaryTreeNode();
        if(btnode)
        {
                btnode->value = value;
                btnode->left = NULL;
                btnode->right = NULL;
        }
        return btnode;
}

void CreateBTree(BTN* root, BTN* left, BTN* right)
{
        if(root)
        {
                root->left = left;
                root->right =right;
        }
}

void DeleteNode(BTN* root)
{
        if(root == NULL)
                return ;
        if(root->left) DeleteNode(root->left);
        if(root->right) DeleteNode(root->right);
        delete root;
        root = NULL;
}
cpp文件

[/u1/yyang/study/algorithm/binarytree] (208)yyang@dcmvrh12#cat rectranverse.cpp
#include "binarytree.h"

void preOrder(BTN* root)
{
        if(root == NULL)
                return;
        printf(" %d ",root->value);
        preOrder(root->left);
        preOrder(root->right);
}

void  inOrder(BTN* root)
{
        if(root == NULL)
                return;
        inOrder(root->left);
        printf(" %d ",root->value);
        inOrder(root->right);
}

void postOrder(BTN* root)
{
        if(root == NULL)
                return;
        postOrder(root->left);
        postOrder(root->right);
        printf(" %d ",root->value);
}

int main()
{

        //            1
        //         /      \
        //        2        3
        //       /\         \
        //      4  5         6
       //         /
        //        7
       //create BTN*node
        BTN* btnode1 = CreateBTNode(1);
        BTN* btnode2 = CreateBTNode(2);
        BTN* btnode3 = CreateBTNode(3);
        BTN* btnode4 = CreateBTNode(4);
        BTN* btnode5 = CreateBTNode(5);
        BTN* btnode6 = CreateBTNode(6);
        BTN* btnode7 = CreateBTNode(7);

        CreateBTree(btnode1,btnode2,btnode3);
        CreateBTree(btnode2,btnode4,btnode5);
        CreateBTree(btnode3,NULL,btnode6);
        CreateBTree(btnode5,btnode7,NULL);

        printf("preOrder is :\n");
        preOrder(btnode1);

        printf("\ninOrder is :\n");
        inOrder(btnode1);

        printf("\npostOrder is :\n");
        postOrder(btnode1);
        printf("\n");
        //free the node
        DeleteNode(btnode1);
}
编译和运行结果:

[/u1/yyang/study/algorithm/binarytree](209)yyang@dcmvrh12#g++ rectranverse.cpp -o rectranverse
[/u1/yyang/study/algorithm/binarytree](210)yyang@dcmvrh12#./rectranverse
preOrder is :
 1  2  4  5  7  3  6
inOrder is :
 4  2  7  5  1  3  6
postOrder is :
 4  7  5  2  6  3  1
结果正确。





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值