Binary | binary tree hierarchy traversal

Question: binary tree hierarchy traversal

Problem link
Here Insert Picture Description
Here Insert Picture Description

Problem-solving ideas

Use two queues and statistics of the number of variables cnt1, cnt2. cnt1 represents the number of nodes of the current layer, initially 1, i.e., the root node. cnt2 represents the number of nodes in the next layer.

C ++ code

/**
 * 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) return ans;
        int cnt1 = 1, cnt2 = 0;//本层节点和下层节点的数量
        queue<TreeNode*>q;//队列
        q.push(root);//压入根节点
        while(!q.empty()){
            vector<int>v; 
            //将本层的cnt1个节点出队列,并统计下一层的节点数量
            while(cnt1--){
                TreeNode *tmp = q.front();
                q.pop();
                v.push_back(tmp->val);
                if(tmp->left){
                    q.push(tmp->left);//加入左孩子
                    cnt2++;//下一层节点数加一
                }
                if(tmp->right){
                    q.push(tmp->right);//加入右孩子
                    cnt2++;//下一层节点数加一
                }
            }
            ans.push_back(v);//将本层的统计记过放入结果ans中
            cnt1 = cnt2;//更新cnt1
            cnt2 = 0;//重置cnt2
        }
        return ans;
    }
};

Question: binary tree hierarchy traversal II

Problem linkHere Insert Picture Description
Here Insert Picture Description

Problem-solving ideas

And "binary tree hierarchy traversal" is almost a question, just talked about what can be reversed.

C ++ code

/**
 * 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>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> ans;
        if(!root) return ans;
        int cnt1 = 1, cnt2 = 0;//本层节点和下层节点的数量
        queue<TreeNode*>q;//队列
        q.push(root);//压入根节点
        while(!q.empty()){
            vector<int>v; 
            //将本层的cnt1个节点出队列,并统计下一层的节点数量
            while(cnt1--){
                TreeNode *tmp = q.front();
                q.pop();
                v.push_back(tmp->val);
                if(tmp->left){
                    q.push(tmp->left);//加入左孩子
                    cnt2++;//下一层节点数加一
                }
                if(tmp->right){
                    q.push(tmp->right);//加入右孩子
                    cnt2++;//下一层节点数加一
                }
            }
            ans.push_back(v);//将本层的统计记过放入结果ans中
            cnt1 = cnt2;//更新cnt1
            cnt2 = 0;//重置cnt2
        }
        //将结果反转一下
        int i = 0, j = ans.size()-1;
        while(i < j){
            vector<int> tmp = ans[i];
            ans[i] = ans[j];
            ans[j] =tmp;
            i++, j--;
        }
        return ans;
    }
};

Question: binary tree zigzag traverse the level

Problem link
Here Insert Picture Description

Problem-solving ideas

Of the binary tree hierarchy traversal resulting vector was subjected to reverse some elements of

C ++ code

/**
 * 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>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int>> ans;
        if(!root) return ans;
        int cnt1 = 1, cnt2 = 0;//本层节点和下层节点的数量
        queue<TreeNode*>q;//队列
        q.push(root);//压入根节点
        while(!q.empty()){
            vector<int>v; 
            //将本层的cnt1个节点出队列,并统计下一层的节点数量
            while(cnt1--){
                TreeNode *tmp = q.front();
                q.pop();
                v.push_back(tmp->val);
                if(tmp->left){
                    q.push(tmp->left);//加入左孩子
                    cnt2++;//下一层节点数加一
                }
                if(tmp->right){
                    q.push(tmp->right);//加入右孩子
                    cnt2++;//下一层节点数加一
                }
            }
            ans.push_back(v);//将本层的统计记过放入结果ans中
            cnt1 = cnt2;//更新cnt1
            cnt2 = 0;//重置cnt2
        }
        //进行反转操作:对下标是奇数的链表进行反转
        for(int i = 1; i < ans.size(); i += 2) reverse(ans[i].begin(), ans[i].end());
        return ans;
    }
};
Published 860 original articles · won praise 270 · views 280 000 +

Guess you like

Origin blog.csdn.net/SongBai1997/article/details/104713323