zoukankan      html  css  js  c++  java
  • 二叉树的遍历

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

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

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

    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;
    }
  • 相关阅读:
    内置函数(少量)
    画国旗(尺寸不标准)
    测试题——程序
    乱码笔记2--字典
    列表
    课堂笔记 ——————乱
    如何利用pip安装国内镜像源
    pip常用指令
    pip卸载
    pip简介
  • 原文地址:https://www.cnblogs.com/cshg/p/6576728.html
Copyright © 2011-2022 走看看