【面試】二叉樹遍歷的非遞歸實現

 


面試時,通常都會對二叉樹進行考察,其中二叉樹遍歷的非遞歸方法常常會被面試官提到(由於遞歸方法太簡單了,有時不懂也能夠寫出來),本文主要講二叉樹遍歷的非遞歸方法(過段時間會整理出來一個關於二叉樹面試的專題,本文將做爲其中的一部分)。ios


 1. 二叉樹遍歷的非遞歸實現

void Bst::preOrderWithoutRecursion(BstNode* root){
    if(root == NULL)
        return ;
    BstNode* pCur = root;
    stack<BstNode*> s;
    while(pCur || !s.empty()){
        while(pCur){
            s.push(pCur);
            cout<<pCur->val<<" ";
            pCur = pCur->left;
        }
        if(!s.empty()){
            pCur = s.top();
            s.pop();
            pCur = pCur->right;
        }
    }
}

void Bst::inOrderWithoutRecursion(BstNode* root){
    if(root == NULL)
        return ;
    stack<BstNode*> s;
    BstNode* pCur = root;
    while(pCur || !s.empty()){
        while(pCur){
            s.push(pCur);
            pCur = pCur->left;
        }
        if(!s.empty()){
            pCur = s.top();
            cout<<pCur->val<<" ";
            s.pop();
            pCur = pCur->right;
        }
    }
}

void Bst::postOrderWithoutRecursion(BstNode* root){
    if(root == NULL)
        return ;
    stack<BstNode*> s;
    BstNode* pCur = root;
    BstNode* pLast = NULL;

    while(pCur){
        s.push(pCur);
        pCur = pCur->left;
    }

    while(!s.empty()){
        pCur = s.top();
        s.pop();
        if(pCur->right == NULL || pCur->right == pLast){
            cout<<pCur->val<<" ";
            pLast = pCur;
        }
        else if(pCur->left == pLast){
            s.push(pCur);
            pCur = pCur->right;
            while(pCur){
                s.push(pCur);
                pCur = pCur->left;
            }
        }
    }
}

2. 完整測試代碼(可直接運行)

/*
* @Author: z.c.wang
* @Email:  iwangzhengchao@gmail.com
* @Date:   2018-10-12 21:08:15
* @Last Modified time: 2019-03-16 17:20:31
*/

#include<iostream>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;

class BstNode {
public:
    int val;
    BstNode* left;
    BstNode* right;
    BstNode(int x):
        val(x), left(NULL), right(NULL){};
};

class Bst{
public:
    BstNode *root;
    Bst():
        root(NULL){}

    void insert(int value);
    void preOrder(BstNode* root);
    void inOrder(BstNode* root);
    void postOrder(BstNode* root);
    void preOrderWithoutRecursion(BstNode* root);
    void inOrderWithoutRecursion(BstNode* root);
    void postOrderWithoutRecursion(BstNode* root);
};


void Bst::insert(int value){
    BstNode* pRoot = root;
    // 空樹
    if(pRoot == NULL)
        root = new BstNode(value);
    // 非空
    else{
        BstNode* temp;
        while(pRoot!= NULL && pRoot->val!=value){
            temp = pRoot;
            if(pRoot->val > value)
                pRoot = pRoot->left;
            else
                pRoot = pRoot->right;
        }
        pRoot = temp;
        if(pRoot->val > value)
            pRoot->left = new BstNode(value);
        else
            pRoot->right = new BstNode(value);
    }
    cout<<"the value "<<value<<" is inserted."<<endl;
}


void Bst::inOrder(BstNode* root){
    if(root == NULL)
        return ;
    inOrder(root->left);
    cout<<root->val<<" ";
    inOrder(root->right);
}

void Bst::preOrder(BstNode* root){
    if(root == NULL)
        return ;
    cout<<root->val<<" ";
    preOrder(root->left);
    preOrder(root->right);
}

void Bst::postOrder(BstNode* root){
    if(root == NULL)
        return ;
    postOrder(root->left);
    postOrder(root->right);
    cout<<root->val<<" ";
}

void Bst::preOrderWithoutRecursion(BstNode* root){
    if(root == NULL)
        return ;
    BstNode* pCur = root;
    stack<BstNode*> s;
    while(pCur || !s.empty()){
        while(pCur){
            s.push(pCur);
            cout<<pCur->val<<" ";
            pCur = pCur->left;
        }
        if(!s.empty()){
            pCur = s.top();
            s.pop();
            pCur = pCur->right;
        }
    }
}

void Bst::inOrderWithoutRecursion(BstNode* root){
    if(root == NULL)
        return ;
    stack<BstNode*> s;
    BstNode* pCur = root;
    while(pCur || !s.empty()){
        while(pCur){
            s.push(pCur);
            pCur = pCur->left;
        }
        if(!s.empty()){
            pCur = s.top();
            cout<<pCur->val<<" ";
            s.pop();
            pCur = pCur->right;
        }
    }
}

void Bst::postOrderWithoutRecursion(BstNode* root){
    if(root == NULL)
        return ;
    stack<BstNode*> s;
    BstNode* pCur = root;
    BstNode* pLast = NULL;

    while(pCur){
        s.push(pCur);
        pCur = pCur->left;
    }

    while(!s.empty()){
        pCur = s.top();
        s.pop();
        if(pCur->right == NULL || pCur->right == pLast){
            cout<<pCur->val<<" ";
            pLast = pCur;
        }
        else if(pCur->left == pLast){
            s.push(pCur);
            pCur = pCur->right;
            while(pCur){
                s.push(pCur);
                pCur = pCur->left;
            }
        }
    }
}


int main(int argc, char const *argv[])
{
    Bst tree;
    int arr[] = {62, 58, 47, 35, 29,
                 37, 36, 51, 49, 48,
                 50, 56, 88, 73, 99, 93};
    for(int i=0; i<16; i++)
        tree.insert(arr[i]);
    cout<<endl<<"前序遍歷: ";
    tree.preOrder(tree.root);
    cout<<endl<<"前序遍歷: ";
    tree.preOrderWithoutRecursion(tree.root);

    cout<<endl<<"中序遍歷: ";
    tree.inOrder(tree.root);
    cout<<endl<<"中序遍歷: ";
    tree.inOrderWithoutRecursion(tree.root);

    cout<<endl<<"後序遍歷: ";
    tree.postOrder(tree.root);
    cout<<endl<<"後序遍歷: ";
    tree.postOrderWithoutRecursion(tree.root);
    return 0;
}

3. 測試結果

(運行結果中的第一個表示遞歸方法,第二個表示非遞歸方法,可見結果相同)面試

the value 62 is inserted.
the value 58 is inserted.
the value 47 is inserted.
the value 35 is inserted.
the value 29 is inserted.
the value 37 is inserted.
the value 36 is inserted.
the value 51 is inserted.
the value 49 is inserted.
the value 48 is inserted.
the value 50 is inserted.
the value 56 is inserted.
the value 88 is inserted.
the value 73 is inserted.
the value 99 is inserted.
the value 93 is inserted.

前序遍歷: 62 58 47 35 29 37 36 51 49 48 50 56 88 73 99 93 
前序遍歷: 62 58 47 35 29 37 36 51 49 48 50 56 88 73 99 93 
中序遍歷: 29 35 36 37 47 48 49 50 51 56 58 62 73 88 93 99 
中序遍歷: 29 35 36 37 47 48 49 50 51 56 58 62 73 88 93 99 
後序遍歷: 29 36 37 35 48 50 49 56 51 47 58 73 93 99 88 62 
後序遍歷: 29 36 37 35 48 50 49 56 51 47 58 73 93 99 88 62
相關文章
相關標籤/搜索