剑指 Offer 32:从上到下打印二叉树

简介: 剑指 Offer 32:从上到下打印二叉树

剑指 Offer 32 - I. 从上到下打印二叉树

题目链接

class Solution {
public:
    vector<int> levelOrder(TreeNode* root) {
        if(!root) return {};
        queue<TreeNode*> queue;
        vector<int> res;
        queue.push(root);
        while(!queue.empty()){
            TreeNode* cur=queue.front();
            queue.pop();
            res.push_back(cur->val);
            if(cur->left){
                queue.push(cur->left);
            }
            if(cur->right){
                queue.push(cur->right);
            }
        }
        return res;
    }
};

剑指 Offer 32 - II. 从上到下打印二叉树 II

题目链接

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        if(!root) return {};
        queue<TreeNode*> queue;
        queue.push(root);
        vector<vector<int>> res;
        while(!queue.empty()){
            int l=queue.size();
            vector<int> tmp;
            for(int i=0;i<l;i++){
                TreeNode* cur=queue.front();
                queue.pop();
                tmp.push_back(cur->val);
                if(cur->left){
                    queue.push(cur->left);
                }
                if(cur->right){
                    queue.push(cur->right);
                }
            }
            res.push_back(tmp);
        }
        return res;
    }
};

剑指 Offer 32 - III. 从上到下打印二叉树 III

题目链接

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        if(!root) return {};
        queue<TreeNode*> queue;
        queue.push(root);
        vector<vector<int>> res;
        int flag=0;
        while(!queue.empty()){
            int l=queue.size();
            vector<int> tmp;
            for(int i=0;i<l;i++){
                TreeNode* cur=queue.front();
                queue.pop();
                tmp.push_back(cur->val);
                if(cur->left){
                    queue.push(cur->left);
                }
                if(cur->right){
                    queue.push(cur->right);
                }
            }
            if(flag&1==1){
                reverse(tmp.begin(),tmp.end());
            }
            res.push_back(tmp);
            flag++;
        }
        return res;
    }
};


相关文章
|
7月前
剑指 Offer 34:二叉树中和为某一值的路径
剑指 Offer 34:二叉树中和为某一值的路径
37 0
|
7月前
leetcode 剑指 Offer 32 - II. 从上到下打印二叉树 II
leetcode 剑指 Offer 32 - II. 从上到下打印二叉树 II
76 0
|
7月前
/leetcode 剑指 Offer 32 - I. 从上到下打印二叉树
/leetcode 剑指 Offer 32 - I. 从上到下打印二叉树
38 0
|
7月前
leetcode 剑指 Offer 32 - III. 从上到下打印二叉树 III
leetcode 剑指 Offer 32 - III. 从上到下打印二叉树 III
49 0
力扣 剑指 Offer 32 - I. 从上到下打印二叉树
力扣 剑指 Offer 32 - I. 从上到下打印二叉树
61 0
力扣 剑指 Offer 32 - III. 从上到下打印二叉树 III
力扣 剑指 Offer 32 - III. 从上到下打印二叉树 III
54 0
力扣 剑指 Offer 32 - II. 从上到下打印二叉树 II
力扣 剑指 Offer 32 - II. 从上到下打印二叉树 II
63 0
|
算法
图解LeetCode——剑指 Offer 34. 二叉树中和为某一值的路径
图解LeetCode——剑指 Offer 34. 二叉树中和为某一值的路径
70 1
|
算法
图解LeetCode——剑指 Offer 32 - III. 从上到下打印二叉树 III
图解LeetCode——剑指 Offer 32 - III. 从上到下打印二叉树 III
87 0
剑指 Offer 32 - II. 从上到下打印二叉树 II(简单,层序遍历)
剑指 Offer 32 - II. 从上到下打印二叉树 II(简单,层序遍历)
46 0
剑指 Offer 32 - II. 从上到下打印二叉树 II(简单,层序遍历)