Advertisement

C++ 二叉树及其镜像算法题解答

阅读量:

二叉树

  • 广度优先搜索:队列结构
    • 深度优先搜索:递归方式(同时也可以通过栈结构来实现)
复制代码
    #include<iostream>
    #include<queue>
    using namespace std;
    
    struct TreeNode{
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int value){
        this->val = value;
        this->left = nullptr;
        this->right = nullptr;
    }
    };
    //    1
    //  2   3
    // 4 5 6 7
    
    // 前序遍历 中左右
    // 1 2 4 5 3 6 7
    void preOrder(TreeNode* root){
    if(root){
        cout<< root->val << endl;
        preOrder(root->left);
        preOrder(root->right);
    }
    }
    
    // 中序遍历 左中右

全部评论 (0)

还没有任何评论哟~