剑指Offer06

32(1). 从上到下打印二叉树

从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印
在这里插入图片描述

思路:
题目要求的二叉树的 从上至下 打印(即按层打印),又称为二叉树的 广度优先搜索(BFS)。BFS 通常借助 队列 的先入先出特性来实现。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> levelOrder(TreeNode* root) {
        vector<int> res;
        if(root == nullptr)
            return res;
        deque<TreeNode*> dq;
        dq.push_back(root);
        while(!dq.empty()){
            TreeNode* node = dq.front();
            dq.pop_front();
            res.push_back(node->val);
            if(node->left){
                dq.push_back(node->left);
            }
            if(node->right){
                dq.push_back(node->right);
            }
        }
        return res;
    }
};
32(2). 从上到下打印二叉树

从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。
在这里插入图片描述

思路:
I. 按层打印: 题目要求的二叉树的 从上至下 打印(即按层打印),又称为二叉树的 广度优先搜索(BFS)。BFS 通常借助 队列 的先入先出特性来实现。
II. 每层打印到一行: 将本层全部节点打印到一行,并将下一层全部节点加入队列,以此类推,即可分为多行打印。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> res;
        vector<int> temp;
        if(root == nullptr)
            return res;
        deque<TreeNode*> dq;
        dq.push_back(root);
        int nextLevel = 0;
        int flag = 1;
        while(!dq.empty())
        {
            TreeNode* node = dq.front();
            temp.push_back(node->val);
            if(node->left){
                dq.push_back(node->left);
                ++nextLevel;
            }
            if(node->right){
                dq.push_back(node->right);
                ++nextLevel;
            }
            dq.pop_front();
            --flag;
            if(flag == 0){
                res.push_back(temp);
                temp.clear();
                flag = nextLevel;
                nextLevel = 0;
            }
        }
        return res;
    }
};
32(3). 从上到下打印二叉树

请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印,其他行以此类推。
在这里插入图片描述

思路:
利用双端队列的两端皆可添加元素的特性,设打印列表(双端队列) tmp ,并规定: 奇数层 则添加至 tmp 尾部 , 偶数层 则添加至 tmp 头部 。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> res;
        vector<int> temp;
        if(root == nullptr)
            return res;
        deque<TreeNode*> dq;
        dq.push_back(root);
        int nextLevel = 0;
        int flag = 1;
        int r_flag = 1;
        while(!dq.empty())
        {
            if(r_flag % 2 == 0)
            {
                TreeNode* node1 = dq.front();
                temp.push_back(node1->val);
                if(node1->right){
                    dq.push_back(node1->right);
                    ++nextLevel;
                }
                if(node1->left){
                    dq.push_back(node1->left);
                    ++nextLevel;
                }
                dq.pop_front();
                --flag;
            }else{
                TreeNode* node2 = dq.back();
                temp.push_back(node2->val);
                if(node2->left){
                    dq.push_front(node2->left);
                    ++nextLevel;
                }
                if(node2->right){
                    dq.push_front(node2->right);
                    ++nextLevel;
                }
                dq.pop_back();
                --flag;
            }
            
            if(flag == 0){
                r_flag++;
                res.push_back(temp);
                temp.clear();
                flag = nextLevel;
                nextLevel = 0;
            }
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值