• 二叉树的遍历


    二叉树的先序遍历顺序为:父亲->左儿子->右儿子

    中序遍历顺序为:左儿子->父亲->右儿子

    后序遍历顺序为:右儿子->左儿子->父亲

    struct Node {
        int value;
        Node *left_child;
        Node *right_child;
        Node () {
            left_child = right_child = NULL;
        }
    };
    bool first = true;
    void Pre_order(Node *tree) {
        if(first) {
            cout << tree->value;
            first = false;
        } else cout << " " << tree->value;
    
        if(tree->left_child != NULL) {
            Pre_order(tree->left_child);
        }
        if(tree->right_child != NULL) {
            Pre_order(tree->right_child);
        }
    }
    void Mid_order(Node *tree) {
        if(tree->left_child != NULL) {
            Mid_order(tree->left_child);
        }
        if(first) {
            cout << tree->value;
            first = false;
        } else cout << " " << tree->value;
        if(tree->right_child != NULL) {
            Mid_order(tree->right_child);
        }
    }
    void Pos_order(Node *tree) {
        if(tree->left_child != NULL) {
            Pos_order(tree->left_child);
        }
        if(tree->right_child != NULL) {
            Pos_order(tree->right_child);
        }
        if(first) {
            cout << tree->value;
            first = false;
        } else cout << " " << tree->value;
    }
  • 相关阅读:
    团队作业WEEK3
    团队作业week2#
    团队作业week2
    Team Homework #3
    learning from the previous teams
    Team Homework #2
    Java语法基础相关
    关于Java的专有名词
    图片验证码
    省市二级联动
  • 原文地址:https://www.cnblogs.com/cshg/p/6576728.html
Copyright © 2020-2023  润新知