美文网首页二叉树之下
二叉树的层序遍历

二叉树的层序遍历

作者: leon4ever | 来源:发表于2018-01-17 16:03 被阅读84次

    来自剑指offer 23题:从上往下打印二叉树。
    这里是利用队列来保证层序从上到下,从左到右。

    /*
    * 二叉树的层次遍历
    */
    #include <iostream>
    #include <queue>
    using namespace std;
    
    struct TreeNode{
        int val;
        TreeNode* left, *right;
        TreeNode(int v = 0) : val(v), left(NULL), right(NULL){}
    };
    
    void visit(TreeNode* p){ //访问函数,这里简单只设为是打印
        cout << p->val << " ";
    }
    
    void levelTravel(TreeNode *root){
        if(root == NULL)
            return;
        queue<TreeNode*> Q;
        Q.push(root);      //初始化
    
        while(!Q.empty()){
            TreeNode* cur = Q.front();
            Q.pop();
            visit(cur);
            if(cur->left)   Q.push(cur->left);
            if(cur->right)  Q.push(cur->right);
        }
    }
    
    
    int main(){
        TreeNode n0(0), n1(1), n2(2), n3(3), n4(4),n5(5), n6(6), n7(7),n8(8), n9(9),n10(10);
        n0.left = &n1; n0.right = &n2;
        n1.left = &n3; n1.right = &n4;
        n2.left = &n5; n2.right = &n6;
        n4.left = &n7; n4.right = &n8;
        n5.left = &n9; n5.right = &n10;
    
        levelTravel(&n0);
        cout << endl;
    }
    

    相关文章

      网友评论

        本文标题:二叉树的层序遍历

        本文链接:https://www.haomeiwen.com/subject/acjuoxtx.html