[Daily question] 33: Sequence traversal of binary tree (from top to bottom, from bottom to top)

Title description

Give you a binary tree, please return the node value obtained by traversing in order. (That is, visit all nodes from left to right layer by layer).

Examples:

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 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>> vv;

        if(!root){
            return vv;
        }

        vector<int> temp;
        queue<TreeNode*> qu;
        TreeNode* cur;

        int len = 1;
        qu.push(root);

        while(!qu.empty()){
            for(int i = 0; i < len; i++){
                cur = qu.front();
                temp.push_back(cur->val);
                qu.pop();

                if(cur->left){
                    qu.push(cur->left);
                }
                if(cur->right){
                    qu.push(cur->right);
                }
            }
            vv.push_back(temp);
            temp.clear();
            len = qu.size();
        }

        return vv;
    }
};

Title description

Given a binary tree, return the traversal of its node values ​​from bottom to top. (That is, from the layer where the leaf node is located to the layer where the root node is located, traversing from left to right layer by layer)

E.g:

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

    3
   / \
  9  20
    /  \
   15   7

The hierarchy traversal that returns from bottom to top is:

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

Answer code

class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> vv;

        if(!root){
            return vv;
        }

        vector<int> temp;
        queue<TreeNode*> qu;
        TreeNode* cur;

        int len = 1;
        qu.push(root);

        while(!qu.empty()){
            for(int i = 0; i < len; i++){
                cur = qu.front();
                temp.push_back(cur->val);
                qu.pop();

                if(cur->left){
                    qu.push(cur->left);
                }
                if(cur->right){
                    qu.push(cur->right);
                }
            }
            vv.push_back(temp);
            temp.clear();
            len = qu.size();
        }

        reverse(vv.begin(),vv.end());
        //vector<vector<int>> res;
        //for(int i = 1; i <= vv.size();i++){
        //    res.push_back(vv[vv.size() - i]);
        //}

        return vv;
    }
};

If you have different opinions, please leave a message to discuss ~~

Published 152 original articles · praised 45 · 10,000+ views

Guess you like

Origin blog.csdn.net/AngelDg/article/details/105407598