首页 > 其他 > 详细

用递归做的前序中序后序遍历

时间:2014-05-26 21:36:43      阅读:365      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<numeric>
using namespace std;

class node{
public:
    int val;
    node* left;
    node* right;
    node():val(0),left(NULL),right(NULL){}
};

node* createTree()
{
    node* head = new node[14];
    for(int i = 0;i<10;i++)
    {
        head[i].val = i;
        if(2*i+1 < 10)
        head[i].left = head + 2*i + 1;
        if(2*i+2 < 10)
        head[i].right = head + 2*i + 2;
    }
    return head;
}

void searchfirst(node* head)
{
    if(NULL == head) return;
    else{
    cout<<head->val<<" ";
    searchfirst(head->left);
    searchfirst(head->right);
    }
}

void searchmiddle(node* head)
{
    if(NULL == head) return;
    else{
    searchmiddle(head->left);
    cout<<head->val<<" ";
    searchmiddle(head->right);
    }
}

void searchlast(node* head)
{
    if(NULL == head) return;
    else{
    searchlast(head->left);///别忘了你一直没调通在哪里???
    searchlast(head->right);///不要万不得已,请不要复制自己写过的代码
    cout<<head->val<<" ";
    }
}

int main()
{
 node* t = createTree();
 searchfirst(t); cout<<endl;
 searchmiddle(t); cout<<endl;
 searchlast(t); cout<<endl;
}
bubuko.com,布布扣

不要复制自己写过的代码~

用递归做的前序中序后序遍历,布布扣,bubuko.com

用递归做的前序中序后序遍历

原文:http://www.cnblogs.com/berkeleysong/p/3745873.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!