CPP:Binary Tree

Posted xinyueliu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CPP:Binary Tree相关的知识,希望对你有一定的参考价值。

void preOrderTraversal(BTNode *root) {  
    if(NULL == root) {
        return;
    }

    printf("%d ", root->val);
    preOrderTraversal(root->lChild);
    preOrderTraversal(root->rChild);
}


void inOrderTraversal(BTNode *root) {  
    if(NULL == root) {
        return;
    }

    inOrderTraversal(root->lChild);
    printf("%d ", root->val); 
    inOrderTraversal(root->rChild);
}  


void postOrderTraversal(BTNode *root) {
    if(NULL == root) {
        return;
    }

    postOrderTraversal(root->lChild);
    postOrderTraversal(root->rChild); 
    printf("%d ", root->val);
}


void inOrderTraversalNonRecursive(BTNode *root) {  
    if(NULL == root) {
        return;
    }

    BTNode *curr = root;
    stack<BTree> s;
    while(NULL != curr || !s.empty()) {
        while(curr != NULL) {
            s.push(curr);
            curr = curr->lChild;
        }

        if(!s.empty()){
            curr = s.pop();
            cout<<curr->data<<"  ";
            curr = curr->rChild;
        }
    }
}


void leverTraversal(BTNode* root) {
    if (NULL == root) {
        return root;
    }

    BTNode* pNode = root;
    queue<BTree> queueNode;
    queueNode.push(p);


    while(!queueNode.empty()) {
        pNode = queueNode.pop();
        if(NULL != pNode->lchild) {
            queueNode.push(p->lchild);
        }

        if(visit(pNode->rchild) == 1) {
            queueNode.push(p->rchild);
        }
    }  
}


void depthTraversal(BTNode* root){
    if (NULL == root) {
        return root;
    }

    stack<BTree> stackNode;
    stackNode.push(root);
    Node *node = NULL;

    while(!stackNode.empty()){
        node = stackNode.pop();
        printf("%d ", node->val);
        if(node->rChild){
            stackNode.push(node->rChild);
        }
        if(node->lChild){
            stackNode.push(node->lChild);
        }
    }
}

  

以上是关于CPP:Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp all_binary_paths.cpp

c_cpp binary_search

c_cpp decimal2binary

添加片段时的 FlyOut 菜单设计问题

js+数据库生成三级动态tree导航菜单

this.$el.querySelectorAll is not a function报错解决