二叉树的层级遍历

题目:leetcode 102

给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回其层次遍历结果:

[
  [3],
  [9,20],
  [15,7]
]

最开始可以想到DFS实现:

时间复杂度:O(n) 每个结点被访问一次

空间复杂度:O(n) 用于存储所有节点的数组

class Solution {
public:
    vector<vector<int>>res;
    void dfs(TreeNode *root,int level){
        if(!root)return ;//遇见空指针直接返回
        if(res.size() == level) res.resize(level+1);//扩容
        res[level].push_back(root->val);
        dfs(root->left , level+1);
        dfs(root->right, level+1);
    }
    vector<vector<int>> levelOrder(TreeNode* root) {
        dfs(root,0);
        return res;
    }
};

用队列实现(BFS)

时间复杂度:O(n) 每个结点被访问一次

空间复杂度:O(n) 用于存储所有节点的数组

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>>res;         //用于存储答案
        if(!root)return res;            //判空
        queue<TreeNode *>q;             //用于存储结点
        q.push(root);                   //初始化
        while(!q.empty()){
            int size = q.size();        //记录长度用于每次循环
            vector<int>tem;             //建立临时数组存储这一层的值
            while(size--){
                TreeNode *p = q.front();
                q.pop();
                tem.push_back(p->val);
                if(p->left )q.push(p->left );
                if(p->right)q.push(p->right);
            }
            res.push_back(tem);         //把这一层添加到答案数组中
        }
        return res;
    }
};

总结来说DFS代码简洁,BFS逻辑比较清晰。

理解了这道题还有一些扩展题目可以尝试练习一下:

比如二叉树的锯齿状遍历

[3,9,20,null,null,15,7]

答案:就是第一层从左到右,第二层从右到左,以此类推

[
  [3],
  [20,9],
  [15,7]
]

附上代码:

class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int>>res;
        if(!root)return res;
        deque<TreeNode *>q; //双端队列实现
        q.push_back(root);
        bool index = false;
        TreeNode *p;
        while(!q.empty()){
            vector<int>tem;
            int size = q.size();
            while(size--){    
                if(index){
                    p = q.back();
                    q.pop_back();
                    if(p->right)q.push_front(p->right);
                    if(p->left )q.push_front(p->left );
                }
                else{
                    p = q.front();
                    q.pop_front();
                    if(p->left )q.push_back(p->left);
                    if(p->right)q.push_back(p->right);
                }
                tem.push_back(p->val);
                
            }
            index = !index;
            res.push_back(tem);
        }
        return res;
    }
};

比如从下到上的层级遍历:

[3,9,20,null,null,15,7]

答案:从最下层返回

[

  [15,7]

  [9,20]
  [3]
]

附上代码:

class Solution {
public:
    vector<vector<int>>res;
    void sta(queue<TreeNode *>q){   //利用递归的系统空间栈来实现从下到上
        vector<int>tem;
        queue<TreeNode *>next;
        while(!q.empty()){
            TreeNode *p = q.front();
            q.pop();
            tem.push_back(p->val);
            if(p->left )next.push(p->left );
            if(p->right)next.push(p->right);
        }
        if(!next.empty()){
            sta(next);
        }
        res.push_back(tem);
    }
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        queue<TreeNode *>q;
        if(root){
            q.push(root);
            sta(q);
        }
        return res;
    }
};

实际上这道题可以用普通的层级遍历然后reverse最后结果也可以实现

 

最后有什么错误还请各位指出!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值