zoukankan      html  css  js  c++  java
  • 用递归做的前序中序后序遍历

    #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;
    }

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

    berkeleysong
  • 相关阅读:
    Swift的闭包(一):闭包简介、闭包表达式的优化
    iOS 用户体验之音频
    【iOS】Core Bluetooth
    【iOS】3D Touch
    golang中channel的超时处理
    Objective-C 中的 BOOL
    2015年总结
    load vs. initialize
    正则表达式
    When does layoutSubviews get called?
  • 原文地址:https://www.cnblogs.com/berkeleysong/p/3745873.html
Copyright © 2011-2022 走看看