Coding-Interview-101

Solutions to LeetCode problems filtered with companies, topics and difficulty.

View project on GitHub

Binary Tree Level Order Traversal II


Solution


    /**
    * Definition for a binary tree node.
    * struct TreeNode {
    *     int val;
    *     TreeNode *left;
    *     TreeNode *right;
    *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
    *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    * };
    */
    class Solution {
    public:
        vector<vector<int>> levelOrderBottom(TreeNode* root) {
            stack<vector<int>> s;
            vector<vector<int>> ans;
            if(root == NULL)
                return ans;
            queue<TreeNode*> q;
            q.push(root);
            while(!q.empty()) {
                int size = q.size();
                vector<int> temp;
                while(size) {
                    TreeNode* top = q.front();
                    q.pop();
                    temp.push_back(top -> val);
                    if(top -> left)
                        q.push(top -> left);
                    if(top -> right)
                        q.push(top -> right);
                    size--;
                }
                s.push(temp);
            }
            while(!s.empty()) {
                ans.push_back(s.top());
                s.pop();
            }
            return ans;
        }
    };