Leetcode Problem Solving Report Sword refers to Offer 32 Print binary tree series from top to bottom

Sword Finger Offer 32-I. Print binary tree from top to bottom

Print out each node of the binary tree from top to bottom, and the nodes at the same level are printed in order from left to right.

E.g:

Given a binary tree: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return:

[3,9,20,15,7]

answer:

This is a simple 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> ans;
        if(root==nullptr)
            return ans;
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty()){
    
    			//BFS模板
            TreeNode *temp=q.front();
            q.pop();
            ans.push_back(temp->val);
            if(temp->left!=nullptr)
                q.push(temp->left);
            if(temp->right!=nullptr)
                q.push(temp->right);
        }
        return ans;

    }
};

Sword refers to Offer 32-II. Print binary tree from top to bottom II

The binary tree is printed in layers from top to bottom, the nodes in the same layer are printed in order from left to right, and each layer is printed to one line.

E.g:

Given a binary tree: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

Return the result of its level traversal:

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

answer:

In the previous blog, the traversal was printed on the console through the Visit() function. Although this topic is a simple BFS layer sequence traversal, it is more interesting to separate the data of each layer. A two-dimensional array is used here.

/**
 * 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>> ans;
        if(root==nullptr)
            return ans;
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty()){
    
    					//BFS模板里面嵌套了每层的操作
            vector<int> t;
            for(int i=q.size();i>0;--i){
    
    
                TreeNode *temp=q.front();
                q.pop();
                t.push_back(temp->val);
                if(temp->left)
                    q.push(temp->left);
                if(temp->right)
                    q.push(temp->right);
            }
            ans.push_back(t);
        }
        return ans;
    }
};

Sword refers to Offer 32-III. Print binary tree from top to bottom III

Please implement a function to print the binary tree in zigzag order, that is, the first line is printed from left to right, the second layer is printed from right to left, and the third line is printed from left to right. Others And so on.

E.g:

Given a binary tree: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

Return the result of its level traversal:

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

answer:

Deque double-ended queue is used here

/**
 * 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>> ans;
        if(root==nullptr)
            return ans;
        deque<TreeNode*> q;
        q.push_back(root);
        bool flag=true;
        while(!q.empty()){
    
    					//BFS模板里面嵌套了每层的操作
            vector<int> t;
            for(int i=q.size();i>0;--i){
    
    
                if(flag){
    
    
                    TreeNode *temp=q.front();
                    q.pop_front();
                    t.push_back(temp->val);
                    if(temp->left!=nullptr)
                        q.push_back(temp->left);
                    if(temp->right!=nullptr)
                        q.push_back(temp->right);
                }else{
    
    
                    TreeNode *temp=q.back();
                    q.pop_back();
                    t.push_back(temp->val);
                    if(temp->right!=nullptr)
                        q.push_front(temp->right);
                    if(temp->left!=nullptr)
                        q.push_front(temp->left);
                }
            }
            flag=!flag;
            ans.push_back(t);
        }
        return ans;
    }
};

Guess you like

Origin blog.csdn.net/qq_43477024/article/details/111773307